Skip to main content
Loading

C# Client

When creating C# client applications to work with Aerospike Cloud, use the client object Aerospike.Client.Proxy.AerospikeClientFactory. If you have existing C# client applications, you can modify them to work with Aerospike Cloud with the following factory code.

Using AerospikeClientFactory.cs​

A simple implementation of the standard factory pattern is included with Aerospike.Client.Proxy. This factory returns a class object adhering to the interface IAerospikeClient, or IAsyncClient for an async version. Depending on the boolean value isProxy, CreateClient returns either a native Client object (Aerospike.Client.AerospikeClient) for use with locally hosted Aerospike deployments, or a proxy client (Aerospike.Client.Proxy.AerospikeClientProxy) for use with Cloud deployments. If an async version of the client is needed, CreateAsyncClient returns either a native Client async object (Aerospike.Client.AsyncClient) for use with locally hosted Aerospike deployments, or a proxy async client (Aerospike.Client.Proxy.AsyncClientProxy).

/*  
* Copyright 2012-2023 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

namespace Aerospike.Client.Proxy
{
/// <summary>
/// AerospikeClientFactory generates a new Client of the specified type
/// with the specified ClientPolicy and sets up the cluster using the provided hosts.
/// </summary>
public class AerospikeClientFactory
{
/// <summary>
/// Return an AerospikeClient or AerospikeClientProxy based on isProxy
/// </summary>
/// <param name="policy">client configuration policy, use null for defaults</param>
/// <param name="isProxy">if true, return AerospikeClientProxy otherwise return AerospikeClient</param>
/// <param name="hosts">array of server hosts that the client can connect</param>
public static IAerospikeClient CreateClient(ClientPolicy policy, bool isProxy, params Host[] hosts)
{
if (isProxy)
{
return new AerospikeClientProxy(policy, hosts);
}
else
{
return new AerospikeClient(policy, hosts);
}
}
/// <summary>
/// Return an AsyncClient or AsyncClientProxy based on isProxy.
/// </summary>
/// <param name="policy">client configuration policy, use null for defaults</param>
/// <param name="isProxy">if true, return AsyncClientProxy otherwise return AsyncClient</param>
/// <param name="hosts">array of server hosts that the client can connect</param>
public static IAsyncClient CreateAsyncClient(AsyncClientPolicy policy, bool isProxy, params Host[] hosts)
{
if (isProxy)
{
return new AsyncClientProxy(policy, hosts);
}
else
{
return new AsyncClient(policy, hosts);
}
}
}
}

Program.cs​

When the main program calls the factory, it returns the appropriate client object based on the value of the bool isProxy. You can replace the simple boolean logic with more sophisticated selection logic if necessary.

Replace the placeholder values in the following code with the correct values from your Aerospike Cloud instance.

using Aerospike.Client;
using Aerospike.Client.Proxy;
using System.Threading.Tasks;
async Task Main()
{
const string User = "<<YOUR_USERID_HERE>>"; // API key
const string Password = "<<YOUR_KEY_HERE>>"; // API key's secret
const string HostName = "6335ce8b-f94f-4024-af20-9c3b0a46d78f.stage.aerospike-cloud.net";
const string NameSpace = "aerospike_cloud";
const string SetName = "TestSet1";
ClientPolicy clientPolicy = new()
{
user = User,
password = Password,
tlsPolicy = new TlsPolicy()
};
bool isProxy = true;

var hosts = new Host[]
{
new Host(HostName, HostName, 4000)
};

// Call factory code to create a new client. This should work for both native and proxy clients
IAerospikeClient client = AerospikeClientFactory.CreateClient(clientPolicy, isProxy, hosts);
// Create a key in namespace "sandbox" and set "ufodata"
Key key = new Key(NameSpace, SetName, 5001);
// Create an array of shapes to add to the report map
string[] shape = { "circle", "flash", "disc" };
// Create the report dictionary
var reportMap = new SortedDictionary<string, Object>
{
{"city", "Ann Arbor"},
{"state", "Michigan"},
{"shape", shape},
{"duration", "5 minutes"},
{"summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"}
};
// Create the bins as Bin("binName", value)
Bin occurred = new Bin("occurred", 20220531);
Bin reported = new Bin("reported", 20220601);
Bin posted = new Bin("posted", 20220601);
Bin report = new Bin("report", reportMap, MapOrder.KEY_ORDERED);
// Write the record to Aerospike
try
{
client.Put(null, key, occurred, reported, posted, report);
Record record = client.Get(null, key);
Console.WriteLine($"Create succeeded\nKey: {key.userKey}");
}
catch (AerospikeException ae)
{
Console.WriteLine($"Create Failed\nError: {ae.Message}");
}
finally
{
// Close the client
client.Close();
}
// Alternatively, if an async version of the proxy client is needed
AsyncClientPolicy asyncClientPolicy = new()
{
user = User,
password = Password,
tlsPolicy = new TlsPolicy()
};
IAsyncClient asyncClient = AerospikeClientFactory.CreateAsyncClient(asyncClientPolicy, isProxy, hosts);
using CancellationTokenSource token = new();

// Write the record to Aerospike
try
{
await asyncClient.Put(null, token.Token, key, occurred, reported, posted, report);
Record record = await asyncClient.Get(null, token.Token, key);
Console.WriteLine($"Create succeeded\nKey: {key.userKey}");
}
catch (AerospikeException ae)
{
Console.WriteLine($"Create Failed\nError: {ae.Message}");
}
finally
{
// Close the client
client.Close();
}
}