# Wednesday, 21 November 2018

Azure Functions allow you to declaratively add bindings to external resources by decorating a C# function with binding attributes.

This means you need to write less code and the code you do write will focus more on your business logic than on updating resources.

In this article, I will show you how to add CosmosDB bindings to an Azure function in order to read from and write to a CosmosDB database.

Create an Configure CosmosDB database and collection

See this article to learn how to create a new CosmosDB instance.

Next create a Database and Collection within your CosmosDB. This article describes how to create a CosmosDB Database and Collection; or you can quickly create a Database named "ToDoList" and a Collection named "Items" from the "Quick Start" tab of the CosmosDB database you created, as shown in Fig. 1.

CD01-QuickStart
Fig. 1

As you work with data in this database, you can view the documents on the "Data Explorer" tab, as shown in Fig. 2.

CD02-DataExplorer
Fig. 2

You will need the Connection String of your CosmosDB. You can find two connection strings on the "Keys" tab, as shown in Fig. 3. Copy either one and save it for later.

CD03-Keys
Fig. 3

Visual Studio project

Create a function in Visual Studio 2017. If you base it on the "Azure Functions" template (Fig. 4), it will have many of the necessary references.

CD04-NewAzureFunctionApp
Fig. 4

Open the local.settings.json file and add a key for "CosmosDBConnection", as shown in Fig. 5. Set its value to the connection string you copied from the "Keys" blade above.

CD05-localsettingsjson
Fig. 5

Delete the existing Function1.cs file from the project and add a new function by right-clicking the project in the Solution Explorer and selecting Add | New Function from the context menu, as shown in Fig. 6. Give the function a meaningful name.

CD06-AddFunction
Fig. 6

Repeat this for any function you wish to add.

Create a model of the expected data

CosmosDB is a schemaless document database, meaning that the database engine does not enforce the type of data it accepts. This is distinct from something like SQL Server, which requires you to define in advance the name, data type, and rules of each column you expect to store.

If you want to validate data, you must do so in your application. One way to do this is to create a Model class that matches the expected incoming data.

In my demo, I expect only to store data that looks like the following:

{
"id" : "001",
"description" : "Write blog post",
"isComplete" : false
}
  

So I created the ToDoItem class shown in Listing 1

public class ToDoItem 
 { 
    [JsonProperty("id")] 
    public string Id { get; set; }

    [JsonProperty("description")] 
    public string Description { get; set; }

    [JsonProperty("isComplete")] 
    public bool IsComplete { get; set; } 
 }
  

Listing 1

Insert a document

The code below generates a function to insert a new document into a database. The function is triggered when you send an HTTP POST request to the function's URL (in this case, "api/InserToDoItem). The document will have the value of the JSON

[FunctionName("InsertItem")] 
public static HttpResponseMessage Run( 
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, 
    [CosmosDB( 
        databaseName: "ToDoList", 
        collectionName: "Items", 
        ConnectionStringSetting = "CosmosDBConnection")] 
    out ToDoItem document, 
    ILogger log) 
{ 
    var content = req.Content; 
    string jsonContent = content.ReadAsStringAsync().Result; 
    document = JsonConvert.DeserializeObject<ToDoItem>(jsonContent);

    log.LogInformation($"C# Queue trigger function inserted one row");

    return new HttpResponseMessage(HttpStatusCode.Created); 
}
  

Let's walk through the function.

[FunctionName("InsertItem")]

The name of the function is InsertItem

public static HttpResponseMessage Run(

The Run method executes when the function is triggered. It returns an HTTP Response Message

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP POST (not GET or PUT or any other verb); and that we are not changing the default routing.

[CosmosDB(
      databaseName: "ToDoList",
      collectionName: "Items",
      ConnectionStringSetting = "CosmosDBConnection")]
     out ToDoItem document,        

The second parameter is an output parameter of type ToDoItem. We will populate this with the data in the Request body, so we type it as a ToDoItem. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically insert this document into the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The ConnectionStringSetting argument must match the name  we added     for the connection string in the local.settings.json file, as described above.

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
document = JsonConvert.DeserializeObject<ToDoItem>(jsonContent);

The 3 lines above retrieve the body in the HTTP POST request and convert it to a .NET object of type ToDoItem, which validates that it is the correct format.

log.LogInformation($"C# Queue trigger function inserted one row");

This line is not necessary, but may help us to understand what part of the function executed when we are troubleshooting.

return new HttpResponseMessage(HttpStatusCode.Created);

When the document is successfully inserted, we return an HTTP 201 (Created) status to indicate success.

Retrieve all documents

The following function retrieves all the documents in a container.

    public static class GetItems
    {
        [FunctionName("GetItems")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Items")
            ]IEnumerable<ToDoItem> toDoItems,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItems == null)
            {
                log.LogInformation($"No Todo items found");
            }
            else
            {
                var ltodoitems = (List<ToDoItem>)toDoItems;
                if (ltodoitems.Count == 0)
                {
                    log.LogInformation($"No Todo items found");
                }
                else
                {
                    log.LogInformation($"{ltodoitems.Count} Todo items found");
                }
            }

            return new OkObjectResult(toDoItems);
        }
    }
  

Breaking down this function:

[FunctionName("GetItems")]        

The name of the function is “GetItems”.

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
SqlQuery = "select * from Items") ]IEnumerable<ToDoItem> toDoItems,

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It is a list of objects of type ToDoItem. When serialized, this will be transformed into an array of JSON objects. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The SQlQuery tells what query to run to retrieve the data (in this case, return all the rows)

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");
if (toDoItems == null)
    {
         log.LogInformation($"No Todo items found");
    }
    else
    {
         var ltodoitems = (List<ToDoItem>)toDoItems;
         if (ltodoitems.Count == 0)
        {
            log.LogInformation($"No Todo items found");
        }
        else
        {
             log.LogInformation($"{ltodoitems.Count} Todo items found");
         }
    }

We did not need to write code to query the database. This happens automatically. The code above simply verifies that items were returned and transforms them into  List<ToDoItem> and stores this list in a local variable.

return new OkObjectResult(toDoItems);

We return a 200 (“OK”) HTTP response and the list of items.

Retrieve a single document by its ID

The following function retrieves a single document, given the ID.

    public static class GetItemById
    {
        [FunctionName("GetItemById")]
            public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetItem/{id}")] HttpRequestMessage req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                Id = "{id}")
            ]ToDoItem toDoItem,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItem == null)
            {
                log.LogInformation($"Item not found");
                return new NotFoundObjectResult("Id not found in collection");
            }
            else
            {
                log.LogInformation($"Found ToDo item {toDoItem.Description}");
                return new OkObjectResult(toDoItem);
            }

        }
    }
  

Here are the details of this function:

[FunctionName("GetItemById")]        

The name of the function is “GetItemById”

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
Id = "{id}")
]IEnumerable<ToDoItem> toDoItems,
 

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It will be an object of type ToDoItem. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The id tells the function on which Id to filter the results.  

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");

Debugging information. Not necessary for the operation, but helpful when troubleshooting.  

if (toDoItem == null)
{
    log.LogInformation($"Item not found");
   return new NotFoundObjectResult("Id not found in collection");
}
else
{
    log.LogInformation($"Found ToDo item {toDoItem.Description}");
   return new OkObjectResult(toDoItem);
}

We did not need to write code to query the database. This happens automatically. The code above simply checks if an item was returned matching the ID. If an item is found, we return a 200 (“OK”) HTTP response, along with the item. If no item is returned, we return a 404 (“Not Found) HTTP response.


Retrieve a set of documents using a query

The following function retrieves a set of document. A query tells the function how to filter, sort and otherwise retrieve the documents. In this example, we only want to return documents for which isComplete = true.

    public static class GetCompleteItems
    {
        [FunctionName("GetCompleteItems")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Items i where i.isComplete")
            ]IEnumerable<ToDoItem> toDoItems,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItems == null)
            {
                log.LogInformation($"No complete Todo items found");
            }
            else
            {
                var ltodoitems = (List<ToDoItem>)toDoItems;
                if (ltodoitems.Count == 0)
                {
                    log.LogInformation($"No complete Todo items found");
                }
                else
                {
                    log.LogInformation($"{ltodoitems.Count} Todo items found");
                }
            }

            return new OkObjectResult(toDoItems);
        }
    }
  

We will now explore this function in more detail:   

[FunctionName("GetCompleteItems")]        

The name of the function is “GetCompleteItems”.

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
SqlQuery = "select * from Items i where i.isComplete")
]IEnumerable<ToDoItem> toDoItems,

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It is a list of objects of type ToDoItem. When serialized, this will be transformed into an array of JSON objects. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The SQlQuery tells what query to run to retrieve the data (in this case, return only rows with isComplete=true) It is important to note that I am using the JSON property (“isComplete”), rather than the .NET class property (“IsComplete”) in this query. Even though they differ only in their case, the query is case-sensitive.  

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");
if (toDoItems == null)
    {
         log.LogInformation($"No complete Todo items found");
    }
    else
    {
         var ltodoitems = (List<ToDoItem>)toDoItems;
         if (ltodoitems.Count == 0)
        {
            log.LogInformation($"No complete Todo items found");
        }
        else
        {
             log.LogInformation($"{ltodoitems.Count} Todo items found");
         }
    }

We did not need to write code to query the database. This happens automatically. The code above simply verifies that items were returned and transforms them into  List<ToDoItem> and stores this list in a local variable.

return new OkObjectResult(toDoItems);

We return a 200 (“OK”) HTTP response and the list of items.

Conclusion

Notice that in each of these functions, I did not need to write code to query or update the database. By decorating a parameter with the CosmosDb attribute, the function automatically took care of the database operations.

You can find this code in the CosmosDBBinding solution in my Azure Function demos on GitHub.