The Hazelcast Cloud Developer Hub

Welcome to the Hazelcast Cloud developer hub. You'll find comprehensive guides and documentation to help you start working with Hazelcast Cloud as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

.NET Client

Quick Setup

Follow the below instructions to connect a .NET client to your cluster.

1. Create a cluster as explained in the Create a Cluster section. After you create the cluster, the cluster-specific page will load and cluster's state will be "Pending" for some seconds, as shown below.

2. When the cluster's state becomes "Running", the previously inactive button Configure Client on this page becomes active. Click on it and the following dialog box appears:

3. Click on the .NET tab (initially, the Java tab opens by default) and download the ZIP file as instructed in Step 1 of the dialog box.

4. Extract the ZIP file and run the command, in the extracted folder, shown in Step 2 of the above dialog box.

You should see the log output similar to the following:

Connection Successful! in your logs means that your client application has successfully connected to your cluster in Hazelcast Cloud. The sample code inserts random entries to your cluster as you can see in the logs. In your Hazelcast Cloud console (your cluster's page where you can reach from the "Clusters" top menu), you should see the charts are being populated with metrics of your map, as shown below:

Also, you can see a your client listed under the "Client Statistics" at the bottom of your cluster's page:

Client Code

Now, as we have successfully connected and put data into the cluster, let's review and explain the client code you downloaded.

Go to the directory (extracted from the ZIP you downloaded in the Step 3 above) and locate the client sample code. If you had enabled encryption in your cluster, open the ClientWithSsl/Program.cs file. Otherwise, open the Client/Program.cs file. We will explain both files line by line in the following sections.

Client/Program.cs

This is the downloaded sample client code when your cluster does not have an encryption.

using System;
using System.Threading;
using Hazelcast.Client;
using Hazelcast.Config;

 namespace Client
 {
     class Program
     {
         public static void Main(string[] args)
         {
             Environment.SetEnvironmentVariable("hazelcast.client.cloud.url", "YOUR_DISCOVERY_URL");
             var config = new ClientConfig();
             config.GetGroupConfig()
                 .SetName("YOUR_CLUSTER_NAME")
                 .SetPassword("YOUR_CLUSTER_PASSWORD");
             config.GetNetworkConfig().GetCloudConfig()
                 .SetEnabled(true)
                 .SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN");

             var client = HazelcastClient.NewHazelcastClient(config);
             var map = client.GetMap<string, string>("map");
             map.Put("key", "value");
             if(map.Get("key").Equals("value"))
             {
                 Console.WriteLine("Connection Successful!");
                 Console.WriteLine("Now, `map` will be filled with random entries.");
             }
             else {
                 throw new Exception("Connection failed, check your configuration.");
             }
             var random = new Random();
             while (true) {
                 var randomKey = random.Next(100_000);
                 map.Put("key" + randomKey, "value" + randomKey);
                 map.Get("key" + random.Next(100_000));
                 if(randomKey % 10 == 0 )
                 {
                     Console.WriteLine("map size: {0}", map.Size());
                 }
                 Thread.Sleep(100);
             }
         }
     }
 }

First, we create an empty client configuration using the following line:

var config = new ClientConfig();

Then, we set the cluster name and password via GroupConfig using the following lines:

config.GetGroupConfig()
    .SetName("YOUR_CLUSTER_NAME")
    .SetPassword("YOUR_CLUSTER_PASSWORD");
The cluster name and password are unique to your cluster. The client is authenticated using this password.

We enable the cloud configuration and set the discovery token using the following lines:
config.GetNetworkConfig().GetCloudConfig()
    .SetEnabled(true)
    .SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN");
The discovery token is also unique to your cluster and is used to discover Hazelcast cluster members.

Now, we create the client with `config` using the following line:
var client = HazelcastClient.NewHazelcastClient(config);
This step creates the connection between your application and the cluster.

We get/create a map and put a simple entry ("key", "value") using the following lines, respectively:
var map = client.GetMap<string, string>("map");
map.Put("key", "value");

Then, we check if the entry has been put successfully and let the code throw an exception if the value is not correct using the following lines. Finally, we put random entries with 100-millisecond intervals:

if(map.Get("key").Equals("value"))
{
    Console.WriteLine("Connection Successful!");
    Console.WriteLine("Now, `map` will be filled with random entries.");
}
else {
    throw new Exception("Connection failed, check your configuration.");
}
var random = new Random();
while (true) {
    var randomKey = random.Next(100_000);
    map.Put("key" + randomKey, "value" + randomKey);
    map.Get("key" + random.Next(100_000));
    if(randomKey % 10 == 0 )
    {
        Console.WriteLine("map size: {0}", map.Size());
    }
    Thread.Sleep(100);
}

ClientWithSsl/Program.cs

This is the downloaded sample client code when your cluster has encryption enabled.

using System;
using System.Threading;
using Hazelcast.Client;
using Hazelcast.Config;

namespace Client
{
    class Program
    {
        public static void Main(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.client.statistics.enabled", "true");
            Environment.SetEnvironmentVariable("hazelcast.client.cloud.url", "YOUR_DISCOVERY_URL");
            var config = new ClientConfig();
            config.GetGroupConfig()
                .SetName("YOUR_CLUSTER_NAME")
                .SetPassword("YOUR_CLUSTER_PASSWORD");

            config.GetNetworkConfig().GetCloudConfig()
                .SetEnabled(true)
                .SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN");

            config.GetNetworkConfig().GetSSLConfig()
                .SetEnabled(true)
                .SetProperty(SSLConfig.ValidateCertificateChain, "false")
                .SetProperty(SSLConfig.CertificateFilePath, "client.pfx")
                .SetProperty(SSLConfig.CertificatePassword, "YOUR_SSL_PASSWORD");

            var client = HazelcastClient.NewHazelcastClient(config);
            var map = client.GetMap<string, string>("map");
            map.Put("key", "value");
            if(map.Get("key").Equals("value"))
            {
                Console.WriteLine("Connection Successful!");
                Console.WriteLine("Now, `map` will be filled with random entries.");
            }
            else {
                throw new Exception("Connection failed, check your configuration.");
            }
            var random = new Random();
            while (true) {
                var randomKey = random.Next(100_000);
                map.Put("key" + randomKey, "value" + randomKey);
                map.Get("key" + random.Next(100_000));
                if(randomKey % 10 == 0 )
                {
                    Console.WriteLine("map size: {0}", map.Size());
                }
                Thread.Sleep(100);
            }
        }
    }
}

The only difference between this one and the Client/Program.cs is the lines that enable and configure SSL encryption on the client side:

            config.GetNetworkConfig().GetSSLConfig()
                .SetEnabled(true)
                .SetProperty(SSLConfig.ValidateCertificateChain, "false")
                .SetProperty(SSLConfig.CertificateFilePath, "client.pfx")
                .SetProperty(SSLConfig.CertificatePassword, "YOUR_SSL_PASSWORD");

You may want to move the pfx file to another directory. Then, you need to set CertificateFilePath accordingly.

More Configuration Options

Please refer to the Hazelcast .NET Client Documentation for more information on the configuration options of the client.

Updated about a year ago

.NET Client


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.