首页 > 解决方案 > 在 C# 控制台应用程序中使用 syncgateway 将数据从 Couchbase 服务器拉到 coubase lite

问题描述

我正在尝试在 .Net(Windows 机器)中创建一个简单的控制台应用程序来连接

couchbase lite -> sync-gateway -> couchbase 服务器(托管在远程机器上)。

但数据不会从服务器拉到精简版。

这是我正在使用的示例代码 -

class Program
{
static void Main(string argss)
{
    // Get the database (and create it if it doesn't exist)
    var database = new Database("testdb");         

    // Create replicator to push and pull changes to and from the cloud
    var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/testdb"));
    var replConfig = new ReplicatorConfiguration(database, targetEndpoint);

    // Add authentication
    replConfig.Authenticator = new BasicAuthenticator("System", "welcome");


    // Create replicator (make sure to add an instance or static variable
    // named _Replicator)
    Replicator _Replicator = new Replicator(replConfig);
    _Replicator.AddChangeListener((sender, args) =>
    {
        if (args.Status.Error != null)
        {
            Console.WriteLine($"Error :: {args.Status.Error}");
        }
    });

    _Replicator.Start();


    // Later, stop and dispose the replicator *before* closing/disposing the database


    using (var query = QueryBuilder.Select(SelectResult.All())
        .From(DataSource.Database(database)))
    {
        // Run the query
        Console.WriteLine($"Query :: {query.ToString()}");
        var result = query.Execute();
        Console.WriteLine($"Result ::  {result.Count()}");
    }
}

下面是我用于 sync_gateway 的配置 -

{
"log": ["*"],
"databases": {
"testdb": {
  "server": "http://167.87.206.28:8091",
  "bucket": "Test",
  "username": "System", 
  "password": "welcome", 
  "enable_shared_bucket_access": true, 
  "import_docs": true,
  "num_index_replicas": 0, 
  "users": {
    "GUEST": { "disabled": false, "admin_channels": ["*"] }
  },
  "sync": `function (doc, oldDoc) {
    if (doc.sdk) {
      channel(doc.sdk);
    }
  }`
}
}
}

我正在使用命令启动 sync_gateway -

C:\Program Files\Couchbase\Sync Gateway>sync_gateway.exe ~path\sync-gateway-config.json

当我尝试运行我的控制台应用程序时,不会从服务器中提取数据并且在控制台中出现以下错误 -

在此处输入图像描述

不知道出了什么问题。任何线索将不胜感激!

标签: c#couchbase-litecouchbase-sync-gateway

解决方案


在 sg 配置中添加 user1:

"users": {
    "user1": {"password": "pass", "admin_channels": ["*"]},
    "GUEST": { "disabled": true, "admin_channels": ["*"] }
  }

// Add authentication
replConfig.Authenticator = new BasicAuthenticator("user1", "pass");

推荐阅读