具有Web Api的CouchbaseServer的存储桶“存储桶名称”验证失败

时间:2017-07-15 22:10:32

标签: c# asp.net .net couchbase

我正在尝试使用Web Api设置CouchbaseServer,以便我可以使用N1QL在NOSQL数据库Couchbase上触发基于SQL的查询。但是我在Getting Bucket中遇到了一个例外。 这就是我的CouchBaseConfig的样子:

public static class CouchbaseConfig
    {
        private static readonly List<string> TravelSampleIndexNames = new List<string>
        {
            "def_sourceairport",
            "def_airportname",
            "def_type",
            "def_faa",
            "def_icao",
            "def_city"
        };

        public static void Register()
        {
            var couchbaseServer = ConfigurationManager.AppSettings.Get("CouchbaseServer");
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = new List<Uri> { new Uri(couchbaseServer) }
            });

            var bucketName = ConfigurationManager.AppSettings.Get("CouchbaseTravelBucket");
            var username = ConfigurationManager.AppSettings.Get("CouchbaseUser");
            var password = ConfigurationManager.AppSettings.Get("CouchbasePassword");

            EnsureIndexes(bucketName, username, password);
        }

        private static void EnsureIndexes(string bucketName, string username, string password)
        {
            var bucket = ClusterHelper.GetBucket(bucketName, password);
            var bucketManager = bucket.CreateManager(username, password);

            var indexes = bucketManager.ListN1qlIndexes();
            if (!indexes.Any(index => index.IsPrimary))
            {
                bucketManager.CreateN1qlPrimaryIndex(true);
            }

            var missingIndexes = TravelSampleIndexNames.Except(indexes.Where(x => !x.IsPrimary).Select(x => x.Name)).ToList();
            if (!missingIndexes.Any())
            {
                return;
            }

            foreach (var missingIndex in missingIndexes)
            {
                var propertyName = missingIndex.Replace("def_", string.Empty);
                bucketManager.CreateN1qlIndex(missingIndex, true, propertyName);
            }

            bucketManager.BuildN1qlDeferredIndexes();
            bucketManager.WatchN1qlIndexes(missingIndexes, TimeSpan.FromSeconds(30));
        }

        public static void CleanUp()
        {
            ClusterHelper.Close();
        }

但是当我运行Web应用程序时,我收到以下错误:

AuthenticationException: Authentication failed for bucket 'travel-sample']

Snapshot of the exception

任何人都可以帮我解决这个问题。 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

我通过更改CouchbaseConfig.Register来解决此错误,如下所示:

public static void Register()
{
    var couchbaseServer = ConfigurationManager.AppSettings.Get("CouchbaseServer");
    var username = ConfigurationManager.AppSettings.Get("CouchbaseUser");
    var password = ConfigurationManager.AppSettings.Get("CouchbasePassword");

    ClusterHelper.Initialize(
        new ClientConfiguration
        {
            Servers = new List<Uri> { new Uri(couchbaseServer) },
        },
        new PasswordAuthenticator(username, password));

    var bucketName = ConfigurationManager.AppSettings.Get("CouchbaseTravelBucket");

    EnsureIndexes(bucketName, username, password);
}