In my previous article we discussed about setting local development environment using Cosmos DB Emulator for Windows. With this part 2 of the article, we will cover developing, debugging and integration related aspects of using Cosmos DB Emulator.
Developing with Cosmos DB Emulator
Once you have Cosmos DB emulator installed and running on your machine, you can use any supported Cosmos DB SDK or Cosmos DB REST API to interact with emulator. This process is same as you are using a Cosmos DB cloud service.
Cosmos DB Emulator also provides a build-in visual explorer through which you can view,create and edit collections and documents.
Before you integrate Cosmos DB SDK or Cosmos DB REST API you would need to generate master key for authentication. Unlike cloud service, Cosmos DB emulator only support single fixed account and master key. You would not be able to communicate with Emulator without this master key.
Default Master Key:
Account name: localhost:<port>
Account key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
PS: This key is only to be used in Emulator. You cannot use the same key for Production(Cosmos DB Cloud Service).
Furthermore, if you want to set your own key. You can go to command line references and run DocumentDB.Emulator.exe with sufficient command switch to set your own key. Remember it should meet the key security requirements. See command-line tool reference for more information.
The Azure Cosmos DB Emulator is installed by default to the
C:\Program Files\Azure Cosmos DB Emulator or
C:\Program Files\DocumentDB Emulator directory.
Once you have account name and key, you are good to go with development and debugging using Azure Cosmos DB emulator.
Let us start looking at how to use CosmosDB SDK. Once you add Cosmos DB SDK for .NET from NUGET sources. You would need to import the following namespaces to reference necessary classes.
Simple code to establish connection:
// Connect to the Azure Cosmos DB Emulator running locally use DocumentClient class in :
DocumentClient client = new DocumentClient(
In the above code block we are directly embedding endpoint, key in the source code.But as a suggested approch keeping in mind to easily point to production service would be maintain the key in Web.config appSettings.
<add value="https://localhost:8081/" key="endpoint"/>
<add value="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" key="authKey"/>
Add NuGet reference to Microsoft.Azure.DocumentDB (always use the latest version of the library)
For the ease of this article, I am going to use the existing ToDoList sample from DocumentDB Samples provided by Microsoft. You can originally find the same source from C:\Program Files\DocumentDB Emulator\Packages\DataExplorer\quickstart.
Copy and Unzip DocumentDB-Quickstart-DotNet.zip and open todo.sln in Visual Studio 2017 and your solution structure will look like below:
Now run the application in your Visual Studio.
1. You will see an initial screen:
2. Click on Create New:
3. New record will be added to your Azure Cosmos DB Emulator:
4. To verify in Cosmos DB emulator now open Cosmos DB explorer, click on Collections and Select ToDoList
5.Expand Documents and select item with id:da305da3-c1dc-4e34-94d9-fd7f82d26c58
Hope this article was helpful for you with initial development. Share your feedback through comments and share this to your friends and colleagues.
- DocumentDB REST API – https://docs.microsoft.com/en-us/azure/documentdb/documentdb-resources
- .NET Samples – https://docs.microsoft.com/azure/documentdb/documentdb-dotnet-samples
- Node.js Samples – https://docs.microsoft.com/en-us/azure/documentdb/documentdb-nodejs-samples
- SQL Syntax – https://msdn.microsoft.com/library/azure/dn782250.aspx
- Python Samples – https://docs.microsoft.com/en-us/azure/documentdb/documentdb-python-samples