[CouchbaseNetClient 2.4.0] Exception in Connecting to Couchbase after sometimes

Hi all,

I’m developing an application which is using Couchbase as the caching server. After launching the application on production environment for a day (or sometimes many days), I’ve seen the exception from initializing the Cluster object with the ClientConfiguration. Error message and stack trace are as below:

Exception message: Index was outside the bounds of the array.
Stack trace:

at System.Collections.Generic.Dictionary2.Enumerator.MoveNext() at Couchbase.Configuration.Client.ClientConfiguration.Initialize() at Couchbase.Core.ClusterController.Initialize() at Couchbase.Core.ClusterController..ctor(ClientConfiguration clientConfig, Func2 ioServiceFactory, Func3 connectionPoolFactory, Func5 saslFactory, IByteConverter converter, ITypeTranscoder transcoder)
at Couchbase.Core.ClusterController…ctor(ClientConfiguration clientConfig)
at [MyProject].Common.Caching.Remote.RemoteCache.ReadFromCluster[T](String bucketName, String key) in [MyProject].remoteCaching.cs:line xx

line xx: using (var cluster = new Cluster(clientConfiguration)) { …

The clientConfiguration above contains 4 servers IP addresses which are setup for fail-over purpose.

I’m wondering that this exception never happened immediately after I deployed the application but sometimes later. Does anyone face this problem before ?

Thank you

@pperfectionist

I think the issue here is probably the way you’re using the cluster. It should normally be initialized once in Global.asax during application startup, and closed one during application shutdown. This is much more performant, and should also avoid your initialization problems. ClusterHelper is a class designed to help with this process.

In summary:

  1. Call ClusterHelper.Initialize during startup
  2. Call ClusterHelper.Close during shutdown
  3. Call ClusterHelper.GetBucket each time you need a bucket (do not Dispose the bucket or wrap in using, just let it fall out of scope).

Brant

Thanks a lot Brant. I’ll try this :slight_smile: