Server and bucket connection in .NET 5

I’m trying to follow your instructions in

Not really working out for me.
Can you just upload a fully working webapi .NET5 project?

Also, I would prefer to be able to use a parameter passed to my webapi methods as the bucket name.
And then use it like I did in the v2 SDK instead of using ‘switch’ to use the correct bucket name interface.

var bucket = ClusterHelper.GetBucket(bucketName);
  1. I believe your problem is that you have your named bucket interfaces as a child of another, internal interface. In order to use them with DI on a public class, you’ll need to make the interfaces fully public.
  2. You are also trying to access a constructor-injected field from a static method on the Authentication class, the authenticate method will need to be an instance method.
  3. You can still choose to inject the IBucketProvider rather than a special named interface, and call _bucketProvider.GetBucketAsync("bucketName") to get the desired bucket. Though that approach still requires you to use DI to inject the IBucketProvider.
  4. If you really want to use a static global (I recommend against it due to testing concerns) then you could easily call Cluster.ConnectAsync(...) during startup and store the ICluster in your own global static equivalent to ClusterHelper.

Here’s an example I’m aware of using option 3 above: https://github.com/mgroves/MicroserviceExample

1 Like

Thank you for your reply @btburnett3 .
Still no go.

  1. I removed the interfaces
  2. I changed it to a non-static method.
  3. I inject the IBucketProvider

I am unable to call my authenticate method from Startup.cs
How exactly am I supposed to pass the parameter to the Authenticate class as this is not really DI… Couldn’t find an example for that in documentation.

I suggest that you read up on Dependency Injection in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

This should provide you with a good overview of how to work with getting services, of which the Couchbase SDK is just one example of many. It’s really the underpinning of any ASP.NET Core web application, and you’ll need a good understanding of it to move forward with your application, regardless of whether you use Couchbase or not.

However, I can help some with this particular case. I recommend adding the following line to ConfigureServices to register your Authentication service with the DI system:

services.AddSingleton<Functions.Authentication>();

Then, instead of making a new instance yourself, get it from the DI container when you need it.

app.Use(async (context, next) =>
{
    var authentication = context.RequestServices.GetRequiredService<Functions.Authentication>();
    await authentication.authenticate(context);

   // ...
});

There are also a lot of other, cleaner ways to implement this. Such as a Middleware class that has your implementation, and which accepts Authentication on its constructor: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-5.0

3 Likes