Microsoft Azure Cosmos DB – Build A Task-Based MongoDB Web App With .NET & Azure Portal
14 Jun. 2017 Microsoft Solutions
At this year’s Build developer conference, Microsoft announced a new addition to its Azure database portfolio. It introduced Azure Cosmos DB aimed at large and globally distributed data intensive applications and addressing a lot of developer pain points.
Microsoft’s new database offering provides a 99% uptime and minimal latency SLA. It’s even migrating all the existing document DB (NoSQL database) customers to the new Cosmos DB, free of charge. The new DB is capable of handling any data type including key-value, columnar and graph type. Developers can scale horizontally with ease and associate different geographical regions with the DB account using a single API call.
Azure Cosmos DB
Microsoft Azure Cosmos DB further supports popular database APIs like MongoDB, Gremlin and Azure tables. Take a look at the tutorial below to learn how you can create a Cosmos DB account within the Azure portal and use the MongoDB API to deploy a task-based application:
- Visual Studio 2017 installed on your system – make sure you have enabled ‘Azure Development’ while setting up Visual Studio.
- Azure subscription – you can create a free account before getting started.
Create A Database Account:
- Open a new window and sign into the Azure portal.
- From the left menu, click on New, select Databases and then select Azure Cosmos DB.
- In the New account blade, state the desired configurations for the Azure Cosmos DB account. Azure Cosmos DB allows you select one of the four programming models: Gremlin (graph), SQL (DocumentDB), MongoDB, and Table (key-value). You must choose MongoDB as we are programming through MongoDB API in this tutorial.
Now fill out the New account blade as shown in the picture below. Make sure you fill unique values so that the information do not match with the screenshot exactly.
Setting Suggested Value Description ID Unique Value Enter a unique name for creating an Azure Cosmos DB account. documents.azure.com will be appended to your ID to create a URL. So, choose a unique but identifiable ID. API MongoDB The programming will be done against MongoDB API later in this tutorial. Subscription Your Subscription The Azure subscription that you want to choose for the Azure Cosmos DB account. Resource Group Same value you have entered in ID A new resource group name for your Azure Cosmos DB account. To keep things simple, you may choose the same name as your ID. Location The region closest to your users The geographical location where your Azure Cosmos DB account will be hosted. Choose the location that is in proximity to your users. This will provide them the fastest access to your data.
- Now click on Create to create the account.
- Go to the toolbar and click on Notifications to monitor the deployment process.
- Once the deployment is over, open the new account from All Resources tile.
Clone The Sample Application:
Now clone a MongoDB API app from GitHub, set the connection string, run it, and follow the below steps.
- Open a git terminal window, for example, git bash, and cd to an active directory.
- Run the following command for cloning the sample repository.
git clone https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started.git
- Open the solution file in Visual Studio.
Review The App Code:
A quick review of the code is essential to identify what’s happening in the application. Open the Dal.cs file under DAL directory.
- Initialize the Mongo client.
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, 10255);
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);
settings.Credentials = new List()
new MongoCredential(“SCRAM-SHA-1”, identity, evidence)
MongoClient client = new MongoClient(settings);
- Retrieve database and the collection.
private string dbName = “Tasks”;
private string collectionName = “TasksList”;
var database = client.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection(collectionName);
- Now retrieve all the documents.
Update The Connection String:
Now return to the Azure portal to get information on connection string and copy it into the app.
- In the Azure portal, go to Azure Cosmos DB account, from the left navigation click on Connection String, and then click on Read-write Keys. In the next step, you will have to copy buttons on the right-side of the screen to copy the Username, Password, and Host into the Dal.cs file.
- Again, open the Dal.cs file in the DAL directory.
- Now copy your username value from the portal by using the copy button and use it as a value of the username in your Dal.cs file.
- Similarly, copy the host value using the copy button in the portal and make it a value of the host in your Dal.cs file.
- Then copy the password value from the portal and make it a value for the password in Dal.cs file.
Finally, you have updated your app with all the information it requires to communicate with the Azure Cosmos DB.
Run The Web App:
- In Visual Studio, in Solution Explorer, right-click on the project and click on Manage NuGet Packages.
- Now type MongoDB.Driver in the NuGet Browse box.
- From the results that are derived, install MongoDB.Driver library. This install includes the MongoDB.Driver package as well as all dependencies.
- Now use CTRL + F5 to run the application. Your application will be displayed in the browser.
- Click on Create in the browser and create a couple of new tasks within your application.
Hopefully, this tutorial might have helped you to create an Azure Cosmos DB account of your own and run a web app using the MongoDB API. After running the app, you can import the additional data to your Azure Cosmos DB account and review the SLAs (Service Level Agreements) to ensure business continuity and high application performance. If you’re encountering hassles while creating an account or building an app using Azure Cosmos DB, consult a professional .NET development company like Rishabh Software or drop us a message in chat today.