How to force a retry of bucketProvider.GetBucketAsync() without using the cached "bucket not found" value using DependencyInjection

I setup a .net core API to read/write to the db:

builder.Services.AddCouchbase(builder.Configuration.GetSection("Couchbase"));
builder.Services.AddCouchbaseBucket<INamedBucketProvider>(builder.Configuration.GetValue<string>("BucketName"));

If I do not have the db accessible/running _bucketProvider.GetBucketAsync() will return the bucket not found error. Once the db is accessible/running _bucketProvider.GetBucketAsync() will continue to serve the bucket not found error.

So if the NamedBucketProvider is ever triggered during initial creation and the db isn’t present for some reason the provider is never able to talk to couchbase once the db server is available. The only way to resolve is to restart the API.

@gregadkins

You are 100% correct, it seems like our performance optimization bit us in this particular edge case. I’ve filed a bug: Loading...

In the meantime, the workaround would be to register a custom implementation of IBucketProvider. You can base it on this implementation and just drop the ConcurrentDictionary cache: https://github.com/couchbase/couchbase-net-client/blob/master/src/Couchbase.Extensions.DependencyInjection/Internal/BucketProvider.cs

Just make sure you register the implementation before you call AddCouchbase and it should take precedence over the default implementation.

2 Likes

Awesome, this worked perfectly, I had to update my Couchbase.Extensions.DependencyInjection from 3.2.8 to 3.3.0 since it seemed it was caching the cluster in the provider too… but with the update to 3.3.0 I saw different exceptions so the clusterprovider must of handled that better. In the end this solved my need. Thanks for the help!

2 Likes