I am new to couchbase server using .net sdk


#1

I have created a bucket using couchbase web console with credentials
now when i am trying to open that bucket using couchbasenetclient i am getting below error

c#:

var cluster = new Cluster();
var bucket = cluster.OpenBucket();
if (bucket != null)
{
Console.WriteLine(“success”);
}

at Couchbase.Core.ClusterController.CreateBucket(String bucketName, String password) in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\Core\ClusterController.cs:line 241
at Couchbase.Core.ClusterController.CreateBucket(String bucketName) in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\Core\ClusterController.cs:line 155
at Couchbase.Cluster.OpenBucket() in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\Cluster.cs:line 83
at HelloCouchbase.Program.Main(String[] args) in C:\Users\pavan.COELAB\documents\visual studio 2015\Projects\HelloCouchbase\HelloCouchbase\Program.cs:line 108
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


#2

@pavang14

Looking at the code, it appears that you don’t have any configuration for the cluster. If you don’t provide configuration, it will always try to connect to localhost and won’t have a password.

You should try setting you configuration in XML, then calling ClusterHelper.Initialize(sectionName). Then get the bucket using ClusterHelper.GetBucket(bucketName). I think that will work better for you.

Example XML:
http://developer.couchbase.com/documentation/server/4.0/sdks/dotnet-2.2/configuring-the-client.html

Note that the example is just for the XML. It shows manually creating Cluster objects and using statements, which isn’t really best practice. You should use ClusterHelper instead:
http://developer.couchbase.com/documentation/server/4.0/sdks/dotnet-2.2/cluster-helper.html

Thanks,
Brant


#3

i have already added configuration like this

configuration
configSections
sectionGroup name="couchbase"
section name=“couchbase” type="Couchbase.Configuration.Client.Providers.CouchbaseClientSection, Couchbase.NetClient"
sectionGroup
configSections
couchbase
couchbase useSsl="false"
servers
add uri=“http://127.0.0.1:8091"
servers
buckets
add name=“secondbucket” useSsl=“false” password=“private"
connectionPool name=“custom” maxSize=“10” minSize=“5” sendTimeout=“12000” connectionPool
add
buckets
couchbase
couchbase
startup
supportedRuntime version=“v4.0” sku=”.NETFramework,Version=v4.5.1” />
startup
runtime
assemblyBinding xmlns=“urn:schemas-microsoft-com:asm.v1”>
dependentAssembly>
assemblyIdentity name=“Common.Logging” publicKeyToken=“af08829b84f0328e” culture=“neutral”/>
bindingRedirect oldVersion=“0.0.0.0-3.1.0” newVersion=“3.1.0”/>
dependentAssembly>
dependentAssembly>
assemblyIdentity name=“Common.Logging.Core” publicKeyToken=“af08829b84f0328e” culture=“neutral”/>
bindingRedirect oldVersion=“0.0.0.0-3.1.0” newVersion=“3.1.0”/>
dependentAssembly>
dependentAssembly>
assemblyIdentity name=“Newtonsoft.Json” publicKeyToken=“30ad4fe6b2a6aeed” culture=“neutral”/>
bindingRedirect oldVersion=“0.0.0.0-6.0.8” newVersion=“6.0.8”/>
dependentAssembly>
assemblyBinding>
runtime>
configuration>

private static void Main(string[] args)
{
ClusterHelper.Initialize(“couchbase/couchbase”);
using (var bucket = ClusterHelper.GetBucket(“secondbucket”))
{
var result = bucket.Upsert(“foof”, “bar”);
}
}

error:

at Couchbase.Core.ClusterController.CreateBucket(String bucketName, String password) in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\Core\ClusterController.cs:line 241
at Couchbase.ClusterHelper.b__0(String name) in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\ClusterHelper.cs:line 100
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Couchbase.ClusterHelper.GetBucket(String bucketName) in c:\Users\jmorris\repos\couchbase-net-client\Src\Couchbase\ClusterHelper.cs:line 92
at HelloCouchbase.Program.Main(String[] args) in C:\Users\pavan.COELAB\documents\visual studio 2015\Projects\HelloCouchbase\HelloCouchbase\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


#4

Now i am able to create a bucket and get data using below solution

ClusterHelper.Initialize(“couchbase/couchbase”);
using (Cluster clus = ClusterHelper.Get())
{
IClusterManager cma = clus.CreateManager(“Administrator”, “welcome@123”);
var doc = new Document()
{
Id = “document_id”,
Content = “Hello World!”
};
IBucket newbucket = null;
cma.RemoveBucket(“secondbucket”);
IResult res = cma.CreateBucket(“latestbucket”, 100);
newbucket = clus.OpenBucket(“latestbucket”);
newbucket.Upsert(doc);
var bucketdata=newbucket.Get(“document_id”);


#5

@pavang24

I see part of your problem here. When using ClusterHelper, you should not dispose the bucket. It’s a singleton instance that ClusterHelper will keep track of. Just use GetBucket whenever you need it, and never dispose.

Brant


#6

@pavang14 -

Per @btburnett3’s suggestion, you should remove the using statements and just ClusterHelper.GetBucket so that the bucket instances are cached and not disposed. They are expensive objects to create and should be long-lived through the life of the application.

Read this for more details: http://developer.couchbase.com/documentation/server/4.0/sdks/dotnet-2.2/cluster-helper.html

-Jeff


#7

Hi burnett

i want to connect couhbase cluster server (which is in some other location),what are the minimum configuration are required to connect that server

i have admin credentials, and cluster url .do i need to have bucket credtionals also to connect specific bucket

do we need to have credentials required to connect beer-sample sample bucket ?

give me sample to connect server level couhbase cluster.
Thanks in advance


#8

In Additional to above comment i added couhbase server using .net sdk(visual studio 2015)


#9

@kapilkumar1218,

If you put a password on the bucket, then you’ll need to specify that password when using ClusterHelper.GetBucket(…)


#10

To connect to a Couchbase cluster, you must have firewall ports open to the cluster. The required ports are the ones in the Node-to-Client column of this document: http://developer.couchbase.com/documentation/server/current/install/install-ports.html

If your cluster is built using IP addresses or domain names, those must also be resolvable from your location. For example, if the cluster uses private IPs, i.e. 192.168.0.50, then requests to 192.168.0.50 from your client must reach the server. You can’t use NAT forwarding from a public IP.

To connect to the cluster, you just need the IP of at least one of the nodes (more is better for redundancy). You must provide a password only for password-protected buckets. Information on configuring the client can be found here: http://developer.couchbase.com/documentation/server/current/sdk/dotnet/client-settings.html

By default, the beer-sample bucket doesn’t require a password. It would only require one if you’ve changed that setting manually.

Brant


#11

@kapilkumar1218 -

The minimum configuration required to connect to the cluster is an URI with the IP or hostname and then the bucket name and optional password. For example:

var config = new ClientConfiguration
{
   Servers = new List<Uri>
   {
      new Uri("http://10.142.150.101:8091/")
   }
 };
var cluster = new Cluster(config);
var bucket = cluster.OpenBucket("bucketName", "password");

#12

Thanks jmorris taking your precious time and giving response :slight_smile:


#13

Hi Morris

I have one question related to Testing Perspective

How to Automate Couch base with UFT?


#14

@kapilkumar1218 -

I am not 100% what UFT means?


#15

UFT-unified functional testing .

difference between Insert and Upsert method in Couch base?


#16

@kapilkumar1218,

UFT is a product from HP, right? I can’t find any material specific to using UFT with Couchbase, but I would imagine it works in a similar way to other databases.

An insert will create a document. It will fail if a document with that same key already exists.
An upsert will create a document if a document with that key doesn’t exist, otherwise it will update the document with the key. I recommend checking out the docs for more information on the .NET SDK: https://developer.couchbase.com/documentation/server/4.5/sdk/dotnet/start-using-sdk.html


#17

Why have to say new Cluster() here. If the Cluster is already existing how can I get that and proceed?


#18

Hi @rkbnair

The code Jeff has demonstrated does not create a new cluster, it instead creates an object in the application code that can interact with an existing cluster.

Hope that helps.