How to get the bucket when previously specified with `Cluster.ConnectAsync`

Hi, I initiated the Cluster in DI with the following code:

serviceCollection.AddSingleton<ICouchbaseRepository>(service =>
    var couchbaseOptions = service.GetService<AppSettings>()?.CouchbaseOptions
                            ?? throw new NullReferenceException(nameof(CouchbaseOptions));
    var cluster = Cluster.ConnectAsync(new ClusterOptions
        ConnectionString = couchbaseOptions.ConnectionString,
        Buckets = new List<string> { couchbaseOptions.Bucket }, // See here
        UserName = couchbaseOptions.Username,
        Password = couchbaseOptions.Password
    return new CouchbaseRepository(cluster);

As I specified the bucket name in the initialisation process, how can I access it later without providing the bucket name?

I cannot use something like Cluster.BucketAsync() as it requires the bucket name here (what’s the point of having to specify the name again when I have specified the desired bucket name previously?).

I tried with Cluster.Buckets.GetAllBucketsAsync() but I got the ArgumentNullException; using Cluster.Buckets.GetAllBucketsAsync(new GetAllBucketsOptions()) or Cluster.Buckets.GetAllBucketsAsync(GetAllBucketsOptions.Default) don’t work either (same errors).

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull(Object value, String parameterName)

   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
   at Couchbase.Management.Buckets.BucketManager.GetBucketSettings(JToken json)
   at Couchbase.Management.Buckets.BucketManager.GetAllBucketsAsync(GetAllBucketsOptions options)


  • .NET Core 5
  • CouchbaseNetClient 3.1.3
  • Couchbase Server community-6.6.0
  • Runs on Docker


Personally, I’d recommend using Couchbase.Extensions.DependencyInjection to register onto DI. It has the concept of an INamedBucketProvider interface. This can be used to request particular buckets based on the interface being requested by the service consumer. You just make a different interface for each bucket.

@btburnett3 Hi, I tried to use that NuGet package but it didn’t work for me. For somehow, I cannot access the Configuration.GetSection function as it’s not built during the ConfigureServices process. (I think that’s only possible with ASP.NET not with .NET Core 5?)

Is there anyway to achieve this without using the extension?



That extension is specifically designed for .NET Core, it’s really the approach I’d recommend. You should definitely have access to Configuration within ConfigureServices. Here’s a link to an example.

@btburnett3 Thanks! I’ll try that out. Btw, I also found that it happens with just two lines of code as well. I created another post for this.

@btburnett3 Hi, I looked into your example code. It doesn’t work for me since .NET Core console app doesn’t support useStartup(I’m using this workaround)

I created an example repository for Couchbase with .NET Core 5 (Console App + Dependency Injection) on GitHub so others who stumble upon this problems can use that as reference.

Oh, I didn’t realize you were doing a console app. Thanks for putting up the example, though!