Using .net SDK IBucketProvider .GetBucket("someName","") throws bootstrap error

I have read the following article and followed the example: https://blog.couchbase.com/dependency-injection-aspnet-couchbase/

but the line:
IBucketProvider .GetBucket("myBucketName", "password")
throws a bootstrap error with an inner exception of nullRefereceException.

When i register a named bucket
.AddCouchbaseBucket<IMyBucketName>("myBucketName", "password");

and then inject it i can access any document.
But i don’t want the bucket i use to be decided by the code i need it to be configurable
any way i can define these named buckets in appsettings.json?

Are you using Couchbase Server 5? Can you share the rest of the code you’re using to setup DI?

Version: 4.1.0-5005 Community Edition (build-5005)

startup.cs

             public void ConfigureServices(IServiceCollection services)
		{
			services.AddMvc();

			services.AddCouchbase(Configuration.GetSection("Couchbase"));
                       
                        //When i add this i can access my bucket seccussfully			
                       //services.AddCouchbaseBucket<IStatisticsBucket>("statistics");

		}

appsettings.json

{
	"AppConfiguration": {
		"AppID": "47c9e98ded9f4d4bad31fdf6930807ea",
	},
	"Couchbase": {
		"Servers": [
			"http://couchbase:8091/pools",
			"http://couchbase2:8091/pools",
			"http://couchbase3:8091/pools"
		],
		"UseSsl": false
	}
}	

controller.cs

private readonly IBucket _CacheBucket;

		// This Works
             public ChannelListController(IStatisticsBucket bucketProvider): base()
		{
			_CacheBucket = bucketProvider.GetBucket();
		}


            //This does not
            public ChannelListController(IBucketProvider  bucketProvider): base()
		{
			_CacheBucket = bucketProvider.GetBucket("statistics", "password");
		}

@arthurvaverko,

I’m a little confused: you say that the following code works:

	// This Works
         public ChannelListController(IStatisticsBucket bucketProvider): base()
	{
		_CacheBucket = bucketProvider.GetBucket();
	}

But there is no overload of GetBucket that takes 0 arguments.

no%20overload

So I don’t see how that would be working for you.

To answer your original question, yes, you can certain put a bucket name into the config file and load that from the config file. I don’t think you can do this with Configuration.GetSection alone, you may have to load it separately. I could be wrong, so I’m going to ping @btburnett3 to get his feedback as well.

The INamedBucketProvider implementation does work without a bucket name in GetBucket(). So that part’s fine. But to support that, you must include the section you commented out:

services.AddCouchbaseBucket<IStatisticsBucket>("statistics");

If you want to make the bucket name itself configurable, you can do that as well. This is how we do it:

{
  "Couchbase": {
    // Couchbase settings here
  },
  "BucketNames": {
    "Statistics": "statistics"
  }
}
services.AddCouchbaseBucket<IStatisticsBucket>(config.GetValue<string>("BucketNames:Statistics"));

So this will do the trick but feels like a workaroung .
I qould like to use a plain IBucketProvider in my controller and just passe the buclet name… But for some reason i get an error …

I belive that has todo with the Couchbase config section that missing some parameters … But im not sure what … :confused:

@arthurvaverko

Just using the plain IBucketProvider should work as well, I’m not sure why you’re getting an exception. The named bucket provider uses IBucketProvider behind the scenes, so I would think that if one was giving an error the other would as well.

https://github.com/couchbaselabs/Couchbase.Extensions/blob/master/src/Couchbase.Extensions.DependencyInjection/Internal/NamedBucketProvider.cs

Can you provide the full error and stack trace?