Introduction
Let’s add DocumentDB to your Web App! We will be performing all operations using the Azure CLI, and all of our work will be built using Visual Studio Code on Bash On Windows, Linux, Mac OS, or a container (we’ll containerize our app in a few weeks). If you have not already, make sure you have a .NET Core Web App ready to go!
Prerequisites
- [Required] Azure CLI (install guide).
- [Required] .NET Core (CLI 1.0.0-rc4-004800+).
- [Required] Azure Subscription.
- [Required] git.
- [Required] Docker.
Build
NOTE: all command statements with multiple lines ignore the need for a newline escape.
Create a DocumentDB with Azure CLI
DocumentDB is one of the cutting edge features available in the Azure CLI, so we need to use a nightly (it’s on the way). I am going to use Docker to keep the latest version of Azure CLI separate from my system configuration. However, if you prever to use the latest build on your machine without Docker, you can install the nightly.
1 | docker run -it azuresdk/azure-cli-python:latest |
Next, let’s create a new DocumentDB instance. I am going to add friend-keeping functionality to my app. I want to keep a list of friends, and some information about them: name, email, phone number, etc.
1 | az documentdb create -g DemoGroup -n friendsdocdb |
We can then get the endpoint for the DocumentDB we just created.
1 | $ az documentdb show -g DemoGroup -n friendsdocdb |
We will need this later, so keep it around.
Connect your Web App to DocumentDB
Obtain the primary master key
In order to connect, we need to get our primary master key for the DocumentDB.
1 | az documentdb regenerate-key -g DemoGroup -n friendsdocdb --key-kind primary |
We will need this later, so keep it around.
Add some DocumentDB code
Let’s add DocumentDB capabilities to our app by adding the proper NuGet packages to our project. I have created a nice little library called DocumentDb.Fluent which drastically improves the DocumentDB interaction experience in .NET.
1 | dotnet add package DocumentDb.Fluent |
In any location, you need to create a static DocumentDB connection generator. I added a new class called Helpers
and added my generator; in addition, I created an a Friend
class as my document type.
1 | public static class Helpers |
Next, let’s convert the ValuesController
(from our .NET Core Web App) into a FriendsController
. I also decided to rename ValuesController.cs
to FriendsController.cs
.
You may notice that the DocumentDb.Fluent library makes all of the calls fairly simple and straightforward. If you prefer, each of the methods I call has a synchronous version, as well.
1 | [ ] |
Optional: to pretty print JSON, add a formatter to the middleware in Startup.cs
that looks like this.
1 | services.AddMvc().AddJsonOptions(options => { |
Test
You can use whatever method you prefer to test your new web app interaction with DocumentDB. In my case, I am using curl
with Bash On Windows.
Get friends.
1 | $ curl http://localhost:5000/api/friends |
Add friend.
1 | $ curl -H "Content-Type: application/json" -X POST |
Update friend.
1 | $ curl -H "Content-Type: application/json" -X PUT |
Delete one friend.
1 | $ curl -H "Content-Type: application/json" -X DELETE |
Delete all friends.
1 | $ curl -H "Content-Type: application/json" -X DELETE |
Deploy
Just as we did when we built our app, we can deploy these changes to Azure with git.
1 | # Push this deploy directory to Azure. |
Done
That’s it! In about 10 minutes, we have added DocumentDB functionality to our web app!