首页 > 解决方案 > 先前使用“Cluster.ConnectAsync”指定时如何获取存储桶

问题描述

我使用以下代码在 DI 中启动了集群:

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);
});

由于我在初始化过程中指定了存储桶名称,我以后如何在不提供存储桶名称的情况下访问它?

我不能使用 Cluster.BucketAsync() 之类的东西,因为它在这里需要存储桶名称(当我之前指定了所需的存储桶名称时,必须再次指定名称有什么意义?)。

我试过了,Cluster.Buckets.GetAllBucketsAsync()但我得到了ArgumentNullException;使用Cluster.Buckets.GetAllBucketsAsync(new GetAllBucketsOptions())Cluster.Buckets.GetAllBucketsAsync(GetAllBucketsOptions.Default)不工作(同样的错误)。

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)

环境:

注意:这是来自 Couchbase 论坛的交叉帖子:https ://forums.couchbase.com/t/how-to-get-the-bucket-when-previously-specified-with-cluster-connectasync/29877

标签: c#.net.net-corecouchbase

解决方案


事实证明,这是一个已知的错误GetAllBucketsAsync()


推荐阅读