<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
</ItemGroup>
Connect to a Couchbase Cluster
Introduction
In this lab, you survey and orient yourself to the application code. Then, you create a cluster reference, and write code to open the bucket you created in the prior lab, within this cluster, using the credentials you set up in the prior lab.
What you will gain experience with:
-
Configuring a .NET project to use the Couchbase .NET SDK
-
Configuring the Cluster reference
-
Configuring the Bucket reference
Estimated time to complete: 30 minutes
Instructions
Project setup
Note
|
If you have not yet downloaded the lab project files, return to the labs home page. Scroll down to the section entitled Obtaining the lab exercise files. Follow the instructions for downloading and installing the lab project files. |
Use Case
The goal of the Couchify application is to offer a set of functionality related to Music tracks, playlists and users. This data has been loaded already into a Couchbase database that you have configured and loaded with data. In prior steps, you set up the Couchbase server, created buckets and loaded the data. Now, it is time to start working on your application project.
In order to define a .NET project that will be able to communicate and interact with the Couchbase database and bucket you have created, you will need to first add the Couchbase .NET SDK to the project. Once this is done, you will need to configure some key components that will allow you to connect to the Couchbase cluster and interact with the buckets.
Some key questions to answer before you get started include the following.
-
What version of the Couchbase SDK will you use?
-
What is the host and port of the Couchbase cluster that you can connect to?
-
What is the name of the bucket or buckets you will access data in?
-
What security has been established that allows or restricts you from connecting to the cluster and what are the necessary credentials to connect?
Configure security for your application
-
In the Couchbase UI (http://localhost:8091/ui/index.html), select the Security tab to open User configuration.
-
Add a new user, specifying the username (e.g., "cbuser"), optional full name, and set a password (e.g., "password").
-
Configure this user for Application Access to the couchmusic buckets that were created earlier.
NoteMuch more granularly identified and secured approaches are available in the Enterprise Edition of Couchbase Server. See the Roles Based Access Control (RBAC) sections of the Couchbase Server 6.5 Enterprise Edition documentation for further detail. -
Save your changes to this new account. You should see the following.
-
Make a note of the username generated here as it will be used in following steps and will be asked as a review question for this course.
Survey the couchify application
-
In Visual Studio Code (or other IDE), open the the lab-03 project, and close unrelated projects.
NoteYou will notice the project fails to build. This will be fixed when you configure the project dependencies in the next section. -
Review the code in this project, in light of the descriptions provided in the related video materials. Focus on reviewing these four files.
-
Couchify.Service.csproj
- Configures project dependencies -
App.config
- Configures key settings used by theCouchifyApplication.cs
class -
CouchifyConnectionManager.cs
- Encapsulates the mechanics of connecting to the Couchbase cluster, obtaining a bucket reference and returning the default collection. -
CouchifyApplication.cs
- Main application that will exercise theCouchifyConnectionManager
class
-
Configure the project dependencies
In order to enable the Couchbase SDK in this project, it is important to add a dependency
to the set of project dependencies configured in the couchify.Service.csproj
file.
Open this file and locate the ItemGroup block where you will see one dependency already declared.
You can add the necessary dependency in a couple of ways.
-
Manually add a similar line to the ItemGroup block for the CouchbaseClient
-
Issue the .NET CLI command to add the dependency
Visit this page to find how to add this dependency for the Couchbase SDK. Perform the necessary step to add this dependency.
Configure appsettings.json
-
Open the
appsettings.json
file and locate the various properties. -
Notice that the ConnectionString property has been set.
-
Provide appropriate values for Username, Password, and Bucket properties based on values you set up earlier in this lab. Note that the bucket you will use for this and successive labs is couchmusic2.
Save your work.
Configure Cluster and Bucket
-
Open the
CouchifyConnectionManager.cs
file. Locate the constructor. Note how the properties are initialized from theappsettings.json
file. -
Locate the GetDefaultCollection() method. Take a moment to observe how the configuration settings from the
appsettings.json
file are read and assigned to properties of the CouchbaseConnectionManager. -
Use these properties to connect to the Couchbase cluster using the Cluster object
-
Obtain a reference to the bucket using the BucketName property that has been configured
-
Finally, return an instance of the Default Collection from the bucket reference.
Test the configuration
Once you are confident in the configuration of your appsettings.json
file and
your implementation in CouchifyConnectionManager
class, you are ready to run tests.
-
Run the application by opening a terminal window or command window and issue the command
dotnet run --project Couchify.Service
and observe the display of the playlist name. -
Locate the
Lab03Tests.cs
file under the Couchify.ServiceTest project. -
Run the test from your IDE. The method of execution may be slightly different in other IDEs.
-
If you have set your properties correctly and configured your Cluster and Bucket properly, the tests should pass.
Lab summary
In this lab, you had the opportunity to perform several key tasks as part of setting up and configuring your Couchbase application.
-
Set up your project to utilize the Couchabase .NET SDK
-
Configure properties specific to your specific security and bucket values
-
Define Cluster and Bucket objects using these properties.
This project is a fairly simple console application that is designed to enable you to gain experience with directly configuring a Cluster and obtain a Bucket reference. In later labs, you will use the .NET Dependency Injection features to inject the Couchbase Cluster and Bucket wrapper classes into your ASP.NET project.