azure-cosmos-db
Home > Blog > Microsoft Azure Cosmos DB – Build A Task-Based MongoDB Web App With .NET & Azure Portal

Microsoft Azure Cosmos DB – Build A Task-Based MongoDB Web App With .NET & Azure Portal

14 Jun 2017

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

Azure Cosmos DB

MongoDB Web App With Dot Net

Want to build a web application using .NET for your business, consult our .NET experts now!

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:

Prerequisites:

  • 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:

  1. Open a new window and sign into the Azure portal.
  2. From the left menu, click on New, select Databases and then select Azure Cosmos DB.

    Step-1

  3. 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.

    Step-2

    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.
  4. Now click on Create to create the account.
  5. Go to the toolbar and click on Notifications to monitor the deployment process.

    Step-5

  6. Once the deployment is over, open the new account from All Resources tile.

    Step-6

Clone The Sample Application:

Now clone a MongoDB API app from GitHub, set the connection string, run it, and follow the below steps.

 

  1. Open a git terminal window, for example, git bash, and cd to an active directory.
  2. Run the following command for cloning the sample repository.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started.git

  3. 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.

 

  1. 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);

  2. Retrieve database and the collection.

    private string dbName = “Tasks”;
    private string collectionName = “TasksList”;

     

    var database = client.GetDatabase(dbName);
    var todoTaskCollection = database.GetCollection(collectionName);

  3. Now retrieve all the documents.

    collection.Find(new BsonDocument()).ToList();

Update The Connection String:

Now return to the Azure portal to get information on connection string and copy it into the app.

 

  1. 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.
  2. Again, open the Dal.cs file in the DAL directory.
  3. 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.
  4. 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.
  5. 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:

  1. In Visual Studio, in Solution Explorer, right-click on the project and click on Manage NuGet Packages.
  2. Now type MongoDB.Driver in the NuGet Browse box.
  3. From the results that are derived, install MongoDB.Driver library. This install includes the MongoDB.Driver package as well as all dependencies.
  4. Now use CTRL + F5 to run the application. Your application will be displayed in the browser.
  5. Click on Create in the browser and create a couple of new tasks within your application.

Conclusion

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.

Get a Free Consultation

Talk to our experts to get the best Microsoft Azure solution for your enterprise.