Connecting to buckets

After you create a ClientConfiguration, you can then connect to a cluster by using the Cluster class.

To connect to a bucket, create a Cluster object and pass the ClientConfiguration object as a parameter.


  var config = new ClientConfiguration
  {
    Servers = new List<Uri>
    {
      new Uri("http://192.168.56.101:8091/pools"),
      new Uri("http://192.168.56.102:8091/pools"),
      new Uri("http://192.168.56.103:8091/pools"),
      new Uri("http://192.168.56.104:8091/pools"),
    }
  };

  using (var cluster = new Cluster(config))
  {
    //open buckets here
  }  
			

The default constructor allocates a ClientConnection internally with the default settings, in other words, creating a ClientConfiguration object is required only when you want to override the default settings.

Now that we have Cluster, we can connect to a bucket by calling the Cluster.OpenBucket() method. The default OpenBucket() method will connect to the default bucket, if you wish to open another bucket, you can use one of the overloads which take a bucket name and password to connect to another bucket. It’s important to note, that you do not need to provide a bucket configuration to open another bucket; if one doesn’t exist, the client will clone one from the default configuration.


  using (var cluster = new Cluster(config))
  {
      using (var bucket = cluster.OpenBucket())
      {
          //use bucket here
      }
  }
		

When you call OpenBucket(), internally the client is using the configuration provided to establish the connection between the client and the server. The references are maintained by the Cluster object; it’s the root object and should be long-lived and reused within your application.