【问题标题】:Set Couchbase client bucket password in C# code在 C# 代码中设置 Couchbase 客户端存储桶密码
【发布时间】:2015-12-16 01:28:52
【问题描述】:

在我的 C# 代码中,我想使用可以传入 bucketName 和密码的 ctor 版本创建一个 Couchbase 客户端:

//
// Summary:
//     Initializes a new instance of the Couchbase.CouchbaseClient class using the
//     default configuration and the specified bucket.
//
// Remarks:
//     The configuration is taken from the /configuration/Couchbase section.
public CouchbaseClient(string bucketName, string bucketPassword);

在我的 web.config 文件中,<couchbase> 部分如下所示:

<couchbase>
  <servers bucket="beer-sample" bucketPassword="">
    <add uri="localhost:8091/pools" />
  </servers>
</couchbase>

在代码中,我尝试通过以下方式创建 Couchbase 客户端:

var cc = new CouchbaseClient("beer-sample", "ThePassword");

上述行总是失败并出现错误“无法找到笔记”。任何人都可以帮忙吗?

【问题讨论】:

    标签: c# couchbase


    【解决方案1】:

    首先,您使用的是旧版本的 Couchbase .Net SDK。 CouchbaseClient 是使用 Couchbase 的旧方式。

    请参考新版指南->http://docs.couchbase.com/developer/dotnet-2.1/dotnet-intro.html

    其次,您必须使用您想要的密码创建您的存储桶。

    例如:

        var clientConfiguration = new ClientConfiguration();
        clientConfiguration.Servers = new List<Uri> { new Uri("http://localhost:8091") };
    
        Cluster Cluster = new Cluster(clientConfiguration);
        using (var bucket = Cluster.OpenBucket("bucketpwd", "1234"))
        {
            Console.WriteLine("Bucket Opened");
        }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      一个有点老的话题,但也许它可以帮助有同样问题的人。

      我目前正在使用 couchbase .net 客户端 2.7.5

      var clusterConfig = new ClientConfiguration
      {
          Servers = new List<Uri>{new Uri("http://couchbase0-node.io:8091")},
          PoolConfiguration = new PoolConfiguration()
      };
      
      using (var cluster = new Cluster(clusterConfig))
      {
          var authenticator = new PasswordAuthenticator(username, password);
          cluster.Authenticate(authenticator);
          using (var bucket = cluster.OpenBucket(bucketName))
          {
              // Do something like :
              var data = await bucket.GetAsync<T>(cacheKey);
              // Other staff
          }
      }
      

      查看更多信息:https://docs.couchbase.com/dotnet-sdk/2.7/start-using-sdk.html

      【讨论】:

        【解决方案3】:

        Couchbase Server 3.0/3.1 参考此文档

            ClientConfiguration example
        
        var config = new ClientConfiguration
        {
          Servers = new List<Uri>
          {
            new Uri("http://192.168.56.101:8091/pools"),
            new Uri("http://192.168.56.102:8091/pools"),
            new Uri("http://192.168.56.103:8091/pools"),
            new Uri("http://192.168.56.104:8091/pools"),
          },
          UseSsl = true,
          DefaultOperationLifespan = 1000,
          BucketConfigs = new Dictionary<string, BucketConfiguration>
          {
            {"default", new BucketConfiguration
            {
              BucketName = "default",
              UseSsl = false,
              Password = "",
              DefaultOperationLifespan = 2000,
              PoolConfiguration = new PoolConfiguration
              {
                MaxSize = 10,
                MinSize = 5,
                SendTimeout = 12000
              }
            }}
          }
        };
        
        using (var cluster = new Cluster(config))
        {
          IBucket bucket = null;
          try
          {
            bucket = cluster.OpenBucket();
            //use the bucket here
          }
          finally
          {
            if (bucket != null)
            {
              cluster.CloseBucket(bucket);
            }
           }
          }
        }
        

        http://docs.couchbase.com/developer/dotnet-2.1/configuring-the-client.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 2018-04-25
          • 1970-01-01
          • 2015-11-24
          • 1970-01-01
          相关资源
          最近更新 更多