Install and Start Using the .NET SDK with Couchbase Server

The Couchbase .NET SDK enables you to interact with a Couchbase Server cluster from the .NET Framework using C#.
It offers both a traditional synchronous API and an asynchronous API based on the Task-based Asynchronous Pattern (TAP).

Installing the SDK

The following sections explain in detail how to get started using each method.

Installing the Client

The Couchbase .NET SDK is compatable with both Microsoft .NET Framework 4.5 or and Microsoft .NET Core runtimes.
Microsoft Visual Studio 2013 or later is assumed in this guide, though you should use 2015 or later if possible.
Add the Couchbase .NET SDK to your Visual Studio solution using one of the following methods:

Using NuGet (recommended method)

Downloading and referencing the binaries

Building from source yourself

Information on new features, fixes, known issues as well as information on how to install older release versions is in the release notes.

The following sections explain in detail how to get started using each method.

Using NuGet

For every release, we package the binaries and store the latest version in NuGet.
If you are not familiar with NuGet, it’s the official and most widely supported package manager for Microsoft Visual Studio and .NET in general.
NuGet is a centralized repository for package authors and consumers, and it also defines a suite of tools for authoring and consuming packages.

Using Visual Studio 2013 or later, follow these steps to get started with the Couchbase .NET SDK:

From the IDE, right-click the project you want to add the dependency to.

As an alternative to using the Visual Studio IDE, you can include the binaries by using the Package Manager Console.
The main advantage of using the Package Manager Console is that the NuGet Dialog by default always installs the latest version of the package published to NuGet.org, however the console allows you to define the version of the package you want to include.
For users targeting older builds of the SDK, using the Package Manager Console is the best option.

To use the Package Manager Console to include the SDK in your project:

From the Visual Studio menu bar, click Tools.

Select NuGet Package ManagerPackage Manager Console.

In the console, enter the package installation command:

To install the latest version:

Install-Package CouchbaseNetClient

To install a specific version, include the version parameter.
For example:

Install-Package CouchbaseNetClient -Version 2.0.0 -Pre

Downloading and referencing the binaries

If you do not want to use NuGet to include the Couchbase .NET SDK in your project, you can download and reference the binaries directly.
If you chose this route, you’ll also be responsible for including and resolving dependencies used internally by the SDK.

Building from source

If none of the other installation options suffice or if you want to debug the source or perhaps contribute, building directly from the source is the best option for you.
All source is located on GitHub.

The software provided via NuGet and S3 are the official releases that have been through a rigorous testing process.
Code on GitHub that is not tagged as an official release is still in development.

Navigate to the directory that the source was cloned to and open the solution.

Build the solution.

After you have successfully built the source, it’s then just a matter of referencing the binaries (.DLL files) from your consuming project.

Hello Couchbase

This tutorial creates a simple console application using Visual Studio that illustrates the most basic usage of the Couchbase .NET SDK.

To begin, open Visual Studio and create a new Console Application Project called Couchbase.HelloCouchbase:

This creates a simple executable with a main() method that you can use to try reading and writing from a Couchbase Cluster.

Next, use the NuGet Package Manager to reference the Couchbase .NET SDK and its dependencies:

At this point, you should be ready to go.
Add a Cluster object, which represents a factory and resource manager for Couchbase buckets.
This is added to the Program.cs file that was added automatically by Visual Studio when the project was created:

The instance of Couchbase Server on which the bucket resides is specified as a URI.
The bucket itself is referenced by name.
To connect to a Couchbase bucket, you must use Couchbase Role-Based Access Control (RBAC).
This is fully described in the section 6.0@server:security:security-authorization.adoc.
A username and password for the current user must be specified.
Following successful authentication, the bucket is opened.

Once you have connected to a Couchbase bucket, you can create a document and add it to the database:

First, the code creates a new Document object, types it as dynamic and provides an Id value.
Then, it creates the actual value that will be stored as JSON in Couchbase and assigns it to the Content property.
After the Document object is created, it uses the Upsert() method to store it into the database.
Finally, it checks whether the operation was successful and if it is, does a GetDocument() operation to retrieve the document and formats a string with the Id of the document and the Name property from the Content field (the actual JSON document).

If you build and run this from Visual Studio, you should see the following message output:

Hello Couchbase!

Congratulations, you have successfully created the Hello Couchbase Tutorial!
The full source can be found on GitHub.

API Reference

The API reference is generated for each release and can be found here.

Contributing

Couchbase welcomes community contributions to the .NET SDK.
The .NET SDK source code is available on GitHub.