Multiple Couchbase bucket configuration in .NET

2019-06-21 23:23发布

问题:

I have 2 buckets in Couchbase one is Couchbase type and the other is Memcachced type: when I run my test I get an error: The element servers may only appear once in this section. Below is my config:

  <couchbase>
    <servers bucket="RepositoryCache" bucketPassword="">
      <add uri="http://127.0.0.1:8091/pools/default"/>
    </servers>

    <servers bucket="default" bucketPassword="">
      <add uri="http://127.0.0.1:8091/pools/default"/>
    </servers>
  </couchbase>

 How to configure multiple buckets and resolve the issue? I hv read the manual and I could not find much help.

回答1:

From the documentation, it looks like you can do it like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="couchbase">
      <section name="bucket-a" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
      <section name="bucket-b" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
    </sectionGroup>
  </configSections>

  <couchbase>
    <bucket-a>
      <servers bucket="default">
        <add uri="http://127.0.0.1:8091/pools" />
      </servers>
    </bucket-a>
    <bucket-b>
      <servers bucket="beernique" bucketPassword="b33rs">
        <add uri="http://127.0.0.1:8091/pools" />
      </servers>
    </bucket-b>
  </couchbase>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>


回答2:

I've asked this question earlier Couchbase multiple buckets in .NET app.config but noone answered.

I've got quick look through couchbase .net library's ClientConfigurationSection and in "couchbase" section of config you can define only one server.

So you can define one bucket "default" that will store connection parametrs of another buckets. Or hardcode connection setttings. Or create own xml-file, that will contain connection params and look like your config that posted above.



回答3:

I found a way around for the above issue.

We can use the CouchbaseClient constructor overload and pass in the bucketname and password. Ex: var client = new CouchbaseClient("default","");

It is not needed to put all bucket config's in the app or web.cong files.



回答4:

If you want to still use App|Web.config, you could also just create a second config section as follows:

<section name="otherconfig" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>

<otherconfig>
    <servers bucket="default" bucketPassword="">
      <add uri="http://127.0.0.1:8091/pools"/>
    </servers>
  </otherconfig>

var client = new CouchbaseClient((CouchbaseClientSection)ConfigurationManager.GetSection("otherconfig"));