Cluster for live time of application

We are updating .NET SDK from v2.x to v3.x.

  1. In v2 we used ClusterHelper in Startup.cs that was initialized once on app startup, and then used that instance via dependency injection:

         var clientConfiguration = new ClientConfiguration(couchbaseClientDefinition);
         ClusterHelper.Initialize(clientConfiguration);
    

Can you, please advise what would be the approach to initialize cluster once at app startup vs creating new instance in Provider class when adding/retrieving docs or executing query. (We are using Repository pattern that has methods working with CB.

  1. In v2 SDK handled the connections by picking connection sting itself:

       ​var cluster = new Cluster(new ClientConfiguration
        ​{
            ​Servers = new List<Uri> { new Uri("http://some_address_1:8091"),
                                                                      new Uri("http://some_address_2:8091") }
        ​});
    

in v3 recommendation is:

       ​ var cluster = await Cluster.ConnectAsync("some_address_1, "user", "pass");

Is my understanding correct that in order to achive functionality of v2 (multiple URIs for CB, we should use something like random function to pick URI randomly?

Thank you

@evgeniya.bell

It is still expected that you should have a singleton instance of the Cluster for the application lifetime. We’ve just removed ClusterHelper, preferring to let you use your dependency injection system’s approach for singletons. If you are using Microsoft.Extensions.DependencyInjection (i.e. in .NET Core) then you can use our extension package, Couchbase.Extensions.DependencyInjection. For Ninject, just register with .InSingletonScope(). Other DI systems should have similar methods.

For the connection string, it still supports bootstrap via multiple servers. This is just using the more current syntax for your connection string. Here is an example:

couchbase://server1,server2,server3

Just comma-delimit the list of servers. You typically do not need to include port numbers any more for most use cases. If using TLS, then use “couchbases” instead of “couchbase” in the URL.

2 Likes

Hi, since ClusterHelper was removed, but all the doc for example for Linq2Couchbase still mentions the ClusterHelper
do you have any updated examples of usage of Linq2Couchbase?

thanks

Linq2Couchbase still hasn’t been fully updated to be compatible with SDK 3. We’ll probably be released a beta shortly, but until then you’ll have to build the DLL from source code in GitHub.

Usage is pretty easy, just make sure to call AddLinq when you build your ClusterOptions.

var options = new ClusterOptions().AddLinq();
// Set other settings

After that, you create a BucketContext passing in the IBucket instance you want to query. For how to get that, refer to the standard SDK 3 documentation. You’ll probably use the DI infrastructure if you’re on .NET Core.https://docs.couchbase.com/dotnet-sdk/current/howtos/managing-connections.html#connection-di