Developing Solutions Locally Using Azure CosmosDB In .NET Core

Introduction 

 
Azure CosmosDB is a NoSQL Document database developed by Microsoft. Document databases are becoming very popular these days. They are very fast and give amazing flexibility for changing schema when compared with Relational Databases. Without being bound to the schema we can get the actual view of data. There are many popular cloud-native document databases such as Amazon DynamoDB, Azure ComosDB, Google Cloud Datastore, etc. Using the Cloud Native databases becomes difficult without a subscription. We need to look for possible approaches to save time and money for testing and developing our solutions.
 
In this tutorial, we will learn how to develop solutions in Azure CosmosDB locally. This approach is very useful for beginning your journey without requiring any subscription. It is very easy to set up this emulator. We get the same look and feel as if we are working in Azure. No additional installation is required. We can easily explore the data using a web browser. We will also use CosmosDB SDK for .NET Core and Create a sample database, container, and then a document. 
 

Setup A Local Instance

  1. Download Azure Cosmos Emulator from the link.
  2. Complete the Installation and Launch Azure Cosmos DB Emulator from the Start Menu.

    Developing Solutions Locally Using Azure CosmosDB in .NET Core

  3. Check the URL, https://localhost:8081/_explorer/index.html.

    Developing Solutions Locally Using Azure CosmosDB in .NET Core

  4. (Optional) Click on Explore and Start with Sample Data.

    Developing Solutions Locally Using Azure CosmosDB in .NET Core

Setup Visual Studio Project

  1. Create a new project in Visual Studio.
  2. Right Click on Solution and Select Manage Nuget Packages for Solution.
  3. Search for Microsoft.Azure.Cosmos and click Install.

    Developing Solutions Locally Using Azure CosmosDB in .NET Core
Developing Solution
 
Add the following references:
  1. using Microsoft.Azure.Cosmos;  
  2. using System;  
  3. using System.Threading.Tasks;  
  4. Create the class below: namespace CosmosDBSample {  
  5.     class CosmosHelper {  
  6.         private readonly string URI = "https://localhost:8081";  
  7.         private readonly string PrimaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";  
  8.         private CosmosClient cosmosClient;  
  9.         private Database database;  
  10.         private Container container;  
  11.         private string databaseName = "SimpleDB";  
  12.         private string containerName = "DataContainer";  
  13.         private string partitionPath = "/id";  
  14.         public CosmosHelper() {  
  15.             this.cosmosClient = new CosmosClient(URI, PrimaryKey);  
  16.         }  
  17.         public async Task CreateTestRecord() {  
  18.             this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);  
  19.             this.container = await this.database.CreateContainerIfNotExistsAsync(containerName, partitionPath);  
  20.             await this.container.CreateItemAsync(new {  
  21.                 id = Guid.NewGuid(),  
  22.                     Name = "Varun"  
  23.             });  
  24.         }  
  25.     }  
  26. }   
Now call this code from your main class.
  1. CosmosHelper cosmosHelper = new CosmosHelper();  
  2. await cosmosHelper.CreateTestRecord();   
Now, verify this in https://localhost:8081/_explorer/index.html
 
Developing Solutions Locally Using Azure CosmosDB in .NET Core 
 
We can see from the above screenshot that SimpleDB, DataContainer is automatically created. Along with this document is inserted. If we will execute the above code multiple times only documents will increase since database and container are already created. This was very simple and very straight forward.
 

Summary

 
In this article, we have covered:
  1. How to Setup Azure CosmosDB Emulator Locally.
  2. Installing ComsosDB SDK from Nuget Package in our Visual Studio Project.
  3. Inserting a Sample Record and Verifying it from Web Browser Client. 
Thanks for Reading! Keep exploring and please feel free to share your thoughts on this. Happy Reading!!


Similar Articles