Unable to resolve "GetBucket"

I’m following the example given here https://blog.couchbase.com/dependency-injection-aspnet-couchbase/\

However I’m unable to get my code to build because I can not seem to find the right assembly to resolve the GetBucket method.

This is what I have in my controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ContractManagement.svc.Models;
using ContractManagement.svc.Interfaces;
using Couchbase;
using Couchbase.Management;
using Couchbase.Extensions;
using Couchbase.AspNet;

private readonly IBucket _CMDB; //couchbase bucket
private readonly string password = “”;

    public ContractManagementController(ILogger<ContractManagementController> logger, ICMDBBucketProvider cmdbBucketProvider)
    {
        _logger = logger;
        _CMDB = cmdbBucketProvider.GetBucket("", password);
    }

    [HttpGet]   // 

In my startup I have the following:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCouchbase(Configuration.GetSection(“Couchbase”));
services.AddCouchbaseBucket("");
}
the interface is defined per the blog referenced above:
using Couchbase.Extensions.DependencyInjection;

namespace ContractManagement.svc.Interfaces
{
public interface ICMDBBucketProvider : INamedBucketProvider
{
// nothing goes here
}
}

What am I missing to get this last referenced method resolved?

That blog post is referring to old versions of Couchbase.Extensions.DependencyInjection and the Couchbase .NET SDK.

If you are using the latest version instead, I’d recommend checking out the documentation - Managing Connections using the .NET SDK with Couchbase Server | Couchbase Docs, especially if you are using the .NET SDK 3. The method you want is probably GetBucketAsync("bucketName") or GetBucketAsync() if you are using a named bucket provider.

Thank you for the clarification on that.
While I don’t see it in the docs you referenced. Do I attach the username and password as part of the query string for the couchbase server, using those words?

services.AddCouchbase(Configuration.GetSection("Couchbase"));

That is where you configure Couchbase in DI and provide the connection string, username, and password. In the above example, it will look at appsettings.json for a section of JSON like this:

"Couchbase" : {
  "ConnectionString" : "couchbase://localhost",
  "Username" : "username",
  "Password" : "password"
}

I’ve made your suggested changes but I’m still not able to get this to work. Now when I try to preform GetBucketAsync() the system just seems to hang. No errors or exceptions are being displayed, nor does it seem to timeout. Please note my instance of couchbase is not running on my local but in a container in Azure.

A couple things you can try: start collecting logs to see if anything shows up there - Collecting Information and Logging in the .NET SDK with Couchbase Server | Couchbase Docs

Also, since you mentioned accessing a non-local cluster, it’s possible the networking setup may not be configured. Check out sdk-doctor to see if it finds any issues: SDK Doctor | Couchbase Docs

problem is resolved. I was using the wrong port on the connection string.

1 Like

Hi Elbilo,

Could you please help me with the sample code of how you implemented the GetBucketAsync() method.

Hi @Pradeep_Kumar1,

I think he’s probably referring to the GetBucketAsync method that’s built into the Couchbase.Extension.DependencyInjection library. You can see the implementation here: https://github.com/couchbase/couchbase-net-client/blob/master/src/Couchbase.Extensions.DependencyInjection/Internal/BucketProvider.cs#L20 but you can just add it to your project with NuGet, no need to write one yourself.

But we cant use the GetBucketAsync method in the constructor. So how do we inject the named provider and create a bucket and how do we run the N1QL queries? Any example of that would be great

@Pradeep_Kumar1

Typically, you store the provider in the constructor and then get the bucket in the actual method. Here’s an MVC example:

public class MyController
{
    private readonly INamedBucketProvider _bucketProvider;

    public MyController(INamedBucketProvider bucketProvider)
    {
        _bucketProvider = bucketProvider;
    }

    [HttpGet]
    public async Task<IActionResult> GetValues()
    {
        var bucket = await _bucketProvider.GetBucketAsync();

        // Do logic here...
    }
}

To run N1QL queries, that is typically done via the cluster.

public class MyController
{
    private readonly IClusterProvider _clusterProvider;

    public MyController(IClusterProvider clusterProvider)
    {
        _clusterProvider= clusterProvider;
    }

    [HttpGet]
    public async Task<IActionResult> GetValues()
    {
        var cluster = await _clusterProvider.GetClusterAsync();

        // Do logic here...
    }
}

However, the cluster may also be accessed via the bucket.Cluster property, if that’s easier for your design.

Thanks a lot:)
This is entirely different from the SDK 2.x. In the 2.x version we used to inject the provider and create the bucket in the Constructor and run the N1QL queries at the bucket level. So we need to now know the clusters on which the query needs to run. Previously we just need to know the bucket on which the query needs to execute. Is my understanding correct?

Definitely use the version 3 sdk, Version 2 did not work for me, and I also ran into the outdated docs.

How do we configure the clusters using Dependency Injection. It is a bit weird, the upsert is at bucket level, the get document is at bucket level. Only the N1Ql queries need to be run at cluster level.

There is a cluster object inside the bucket. I just used that property for my n1ql queries, and preformed key-value and subqueries using the bucket. Depending on your situation you can get better performance using these other types of queries.