Hazelcast .NET Client

Elastically scale your .NET application against real-time data extensive requirements with modern asynchronous APIs. Hazelcast is an excellent solution when scaling and speed matter.

  • Access all of the Hazelcast data structures plus distributed queues, topics, and more with the Hazelcast .NET Client
  • Use SQL to run queries over your distributed data.
  • Use the Hazelcast Near Cache feature to store frequently read data in your .NET application. Near Cache provides faster read speeds than traditional caches such as Redis or Memcached. Eventual Consistency is also supported.
  • Provides SSL support and Mutual Authentication for your enterprise-level security needs.
  • Use and query your JSON objects.
  • Zero downtime during upgrades with Blue/Green failover support.
  • Cluster wide synchronization capability with distributed locks powered by CP Subsystem.

Quick Start + Download

Quick Start

From NuGet package manager console:ย Install-Packageย Hazelcast.Net

Downloads

Version Downloads Documentation
Hazelcast .NET/CSharp Client 5.5.1 (latest)
06/26/2024
Hazelcast .NET/CSharp Client 5.5.0
10/14/2024
Hazelcast .NET/CSharp Client 5.4.0
06/21/2024
Hazelcast .NET/CSharp Client 5.3.1
10/04/2023

For all downloads please see the Download Archives

Previous Releases

Code Samples

using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class MapExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // get distributed map from cluster
            await using var map = await client.GetMapAsync<string, string>("my-distributed-map");
 
            // set/get
            await map.SetAsync("key", "value");
            await map.GetAsync("key");
 
            // concurrent methods, optimistic updating
            await map.PutIfAbsentAsync("somekey", "somevalue");
            await map.ReplaceAsync("key", "value", "newvalue");
 
            // destroy the map
            await client.DestroyAsync(map);
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class QueueExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // Get a Blocking Queue called "my-distributed-queue"
            var queue = await client.GetQueueAsync<string>("my-distributed-queue");
            // Offer a String into the Distributed Queue
            await queue.PutAsync("item");
            // Poll the Distributed Queue and return the String
            await queue.PollAsync();
            //Timed blocking Operations
            await queue.OfferAsync("anotheritem", TimeSpan.FromMilliseconds(500));
            await queue.PollAsync(TimeSpan.FromSeconds(5));
            //Indefinitely blocking Operations
            await queue.PutAsync("yetanotheritem");
            Console.WriteLine(await queue.TakeAsync());
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
using Hazelcast.DistributedObjects;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class TopicExample
    {
        private static void OnMessage(IHTopic<string> sender, TopicMessageEventArgs<string> args)
        {
            Console.WriteLine($"Got message " + args.Payload);
        }
 
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // get distributed topic from cluster
            await using var topic = await client.GetTopicAsync<string>("my-distributed-topic");
 
            // subscribe to event
            await topic.SubscribeAsync(on => on.Message(OnMessage));
 
            // publish a message to the Topic
            await topic.PublishAsync("Hello to distributed world");
 
            // allow event to trigger
            await Task.Delay(1_000);
 
            // destroy the topic
            await client.DestroyAsync(topic);
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class ListExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // Get the Distributed List from Cluster.
            await using var list = await client.GetListAsync<string>("my-distributed-list");
 
            // Add elements to the list
            await list.AddAsync("item1");
            await list.AddAsync("item2");
 
            // Remove the first element
            Console.WriteLine("Removed: " + await list.RemoveAsync(0));
            // There is only one element left
            Console.WriteLine("Current size is " + await list.GetSizeAsync());
            // Clear the list
            await list.ClearAsync();
 
            await client.DestroyAsync(list);
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class ReplicatedMapExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // Get a Replicated Map called "my-replicated-map"
            await using var map = await client.GetReplicatedMapAsync<string, string>("my-replicated-map");
            // Put and Get a value from the Replicated Map
            var replacedValue = await map.PutAsync("key", "value"); // key/value replicated to all members
            Console.WriteLine("replacedValue = " + replacedValue); // Will be null as its first update
            var value = await map.GetAsync("key"); // the value is retrieved from a random member in the cluster
            Console.WriteLine("value for key = " + value);
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class SetExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            // Get the Distributed Set from Cluster.
            await using var set = await client.GetSetAsync<string>("my-distributed-set");
 
            // Add items to the set with duplicates
            await set.AddAsync("item1");
            await set.AddAsync("item1");
            await set.AddAsync("item2");
            await set.AddAsync("item2");
            await set.AddAsync("item2");
            await set.AddAsync("item3");
 
            // Get the items. Note that there are no duplicates.
            await foreach (var item in set)
            {
                Console.WriteLine(item);
            }
 
            await client.DestroyAsync(set);
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class MultiMapExample : ExampleBase
    {
        public async Task Run(string[] args)
        {
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(BuildExampleOptions(args));
 
            // Get the Distributed MultiMap from Cluster.
            await using var multiMap = await client.GetMultiMapAsync("my-distributed-multimap");
            // Put values in the map against the same key
            await multiMap.PutAsync("my-key", "value1");
            await multiMap.PutAsync("my-key", "value2");
            await multiMap.PutAsync("my-key", "value3");
            // Print out all the values for associated with key called "my-key"
            var values = await multiMap.GetAsync("my-key");
            foreach (var item in values)
            {
                Console.WriteLine(item);
            }
 
            // remove specific key/value pair
            await multiMap.RemoveAsync("my-key", "value2");
        }
    }
}
View Sample on GitHub
using System;
using System.Threading.Tasks;
 
namespace Hazelcast.Examples.WebSite
{
    // ReSharper disable once UnusedMember.Global
    public class RingBufferExample
    {
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                .With(args)
                .WithConsoleLogger()
                .Build();
 
            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
 
            await using var rb = await client.GetRingBufferAsync<long>("rb");
 
            // add two items into ring buffer
            await rb.AddAsync(100);
            await rb.AddAsync(200);
 
            // we start from the oldest item.
            // if you want to start from the next item, call rb.tailSequence()+1
            var sequence = await rb.GetHeadSequenceAsync();
            Console.WriteLine(await rb.ReadOneAsync(sequence));
            sequence += 1;
            Console.WriteLine(await rb.ReadOneAsync(sequence));
 
            await client.DestroyAsync(rb);
        }
    }
}
View Sample on GitHub