Developing Solutions Locally Using Azure CosmosDB In .NET Core

Developing Solutions Locally Using Azure CosmosDB In .NET Core
Developing Solutions Locally Using Azure CosmosDB In .NET Core


Azure CosmosDB is a NoSQL Document database developed with the aid of Microsoft. Document databases are turning into very famous these days. They are very quick and supply fantastic flexibility for altering schema when in contrast with Relational Databases. Without being certain to the schema we can get the genuine view of data. There are many famous cloud-native file databases such as Amazon DynamoDB, Azure ComosDB, Google Cloud Datastore, etc. Using the Cloud Native databases will become tough except a subscription. We want to seem to be for feasible tactics to keep time and cash for checking out and creating our solutions.

In this tutorial, we will analyze how to improve options in Azure CosmosDB locally. This strategy is very beneficial for commencing your trip barring requiring any subscription. It is very convenient to set up this emulator. We get the identical seem to be and experience as if we are working in Azure. No extra set up is required. We can without problems discover the statistics the use of a net browser. We will additionally use CosmosDB SDK for .NET Core and Create a sample database, container, and then a document.

Setup A Local Instance

  • Download Azure Cosmos Emulator from the link.
  • Complete the Installation and Launch Azure Cosmos DB Emulator from the Start Menu.
  • Check the URL, https://localhost:8081/_explorer/index.html.
  • (Optional) Click on Explore and Start with Sample Data.

Setup Visual Studio Project

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

Developing Solution

Add the following references:

using Microsoft.Azure.Cosmos;  
using System;  
using System.Threading.Tasks;  
Create the class below: namespace CosmosDBSample {  
    class CosmosHelper {  
        private readonly string URI = "https://localhost:8081";  
        private readonly string PrimaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";  
        private CosmosClient cosmosClient;  
        private Database database;  
        private Container container;  
        private string databaseName = "SimpleDB";  
        private string containerName = "DataContainer";  
        private string partitionPath = "/id";  
        public CosmosHelper() {  
            this.cosmosClient = new CosmosClient(URI, PrimaryKey);  
        public async Task CreateTestRecord() {  
            this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);  
            this.container = await this.database.CreateContainerIfNotExistsAsync(containerName, partitionPath);  
            await this.container.CreateItemAsync(new {  
                id = Guid.NewGuid(),  
                    Name = "Varun"  

Now call this code from your main class.

CosmosHelper cosmosHelper = new CosmosHelper();  
await cosmosHelper.CreateTestRecord();   

Now, verify this in https://localhost:8081/_explorer/index.html

We can see from the above screenshot that SimpleDB, DataContainer is routinely created. Along with this file is inserted. If we will execute the above code a couple of instances solely files will enlarge considering the fact that database and container are already created. This used to be very easy and very straight forward.


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

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 7 =

You May Also Like
Twelve-Factor Application of Microservices
Read More

The Twelve-Factor App

Introduction In the current era, software program is usually delivered as a service: referred to as net apps,…