Build A Simple .NET Console App


Build A Simple .NET Console App

After using the Azure Portal’s Data Explorer to query an Azure Cosmos DB container. You are now going to use the .NET SDK to issue similar queries.

If this is your first lab and you have not already completed the setup for the lab content see the instructions for Account Setup before starting this lab.

Create a .NET Core Project

  1. On your local machine, locate the CosmosLabs folder in your Documents folder and open the Lab05 folder that will be used to contain the content of your .NET Core project. If you are completing this lab through Microsoft Hands-on Labs, the CosmosLabs folder will be located at the path: C:\labs\CosmosLabs

  2. In the Lab05 folder, right-click the folder and select the Open with Code menu option.

    Open with Visual Studio Code

    Alternatively, you can run a terminal in your current directory and execute the code . command.

  3. In the Visual Studio Code window that appears, right-click the Explorer pane and select the Open in Terminal menu option.

    Open in Terminal

  4. In the terminal pane, enter and execute the following command:

    dotnet restore
    

    This command will restore all packages specified as dependencies in the project.

  5. In the terminal pane, enter and execute the following command:

    dotnet build
    

    This command will build the project.

  6. Click the đź—™ symbol to close the terminal pane.

  7. In the Explorer pane verify that you have a DataTypes.cs file in your project folder.

    This file contains the data classes you will be working with in the following steps.

  8. Double-click the Program.cs link in the Explorer pane to open the file in the editor.

    Open editor

  9. For the _endpointUri variable, replace the placeholder value with the URI value and for the _primaryKey variable, replace the placeholder value with the PRIMARY KEY value from your Azure Cosmos DB account. Use these instructions to get these values if you do not already have them:

    For example, if your uri is https://cosmosacct.documents.azure.com:443/, your new variable assignment will look like this: private static readonly string _endpointUri = "https://cosmosacct.documents.azure.com:443/";.

    For example, if your primary key is elzirrKCnXlacvh1CRAnQdYVbVLspmYHQyYrhx0PltHi8wn5lHVHFnd1Xm3ad5cn4TUcH4U0MSeHsVykkFPHpQ==, your new variable assignment will look like this: private static readonly string _primaryKey = "elzirrKCnXlacvh1CRAnQdYVbVLspmYHQyYrhx0PltHi8wn5lHVHFnd1Xm3ad5cn4TUcH4U0MSeHsVykkFPHpQ==";.

    We are now going to implement a sample query to make sure our client connection code works.

Read a single Document in Azure Cosmos DB Using ReadItemAsync

ReadItemAsync allows a single item to be retrieved from Cosmos DB by its ID. In Azure Cosmos DB, this is the most efficient method of reading a single document.

  1. Locate the using block within the Main method:

    using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey))
    {
        var database = client.GetDatabase(_databaseId);
        var container = database.GetContainer(_containerId);
    
    }
    
  2. Add the following lines of code to use the ReadItemAsync function to retrieve a single item from your Cosmos DB by its id and write its description to the console.

     ItemResponse<Food> candyResponse = await container.ReadItemAsync<Food>("19130", new PartitionKey("Sweets"));
     Food candy = candyResponse.Resource;
     Console.Out.WriteLine($"Read {candy.Description}");
    
  3. Save all of your open tabs in Visual Studio Code

  4. Return to your terminal pane

    If you’ve closed the terminal right-click the Explorer pane and select the Open in Terminal menu option.

  5. In the open terminal pane, enter and execute the following command:

    dotnet run
    
  6. You should see the following line output in the console, indicating that ReadItemAsync completed successfully:

    Read Candies, HERSHEY''S POT OF GOLD Almond Bar
    

Execute a Query Against a Single Azure Cosmos DB Partition

  1. Return to Visual Studio Code

    If you’ve closed Visual Studio code, re-open it from the command line with the following command:

    code .
    
  2. Find the last line of code you wrote:

    Console.Out.WriteLine($"Read {candy.Description}");
    
  3. Create a SQL Query against your data, as follows:

    string sqlA = "SELECT f.description, f.manufacturerName, f.servings FROM foods f WHERE f.foodGroup = 'Sweets' and IS_DEFINED(f.description) and IS_DEFINED(f.manufacturerName) and IS_DEFINED(f.servings)";
    

    This query will select all food where the foodGroup is set to the value Sweets. It will also only select documents that have description, manufacturerName, and servings properties defined. You’ll note that the syntax is very familiar if you’ve done work with SQL before. Also note that because this query has the partition key in the WHERE clause, this query can execute within a single partition.

  4. Add the following code to execute and read the results of this query

    FeedIterator<Food> queryA = container.GetItemQueryIterator<Food>(new QueryDefinition(sqlA), requestOptions: new QueryRequestOptions{MaxConcurrency = 1});
    foreach (Food food in await queryA.ReadNextAsync())
    {
        await Console.Out.WriteLineAsync($"{food.Description} by {food.ManufacturerName}");
        foreach (Serving serving in food.Servings)
        {
            await Console.Out.WriteLineAsync($"\t{serving.Amount} {serving.Description}");
        }
        await Console.Out.WriteLineAsync();
    }
    
  5. Save all of your open tabs in Visual Studio Code

  6. Return to your terminal pane

    If you’ve closed the terminal right-click the Explorer pane and select the Open in Terminal menu option.

  7. In the open terminal pane, enter and execute the following command:

    dotnet run
    
  8. The code will loop through each result of the SQL query and output a message to the console similar to the following:

    ...
    
    Puddings, coconut cream, dry mix, instant by
        1 package (3.5 oz)
        1 portion, amount to make 1/2 cup
    
    ...
    

Execute a Query Against Multiple Azure Cosmos DB Partitions

  1. Return to Visual Studio Code

    If you’ve closed Visual Studio code, re-open it from the command line with the following command:

    code .
    
  2. Following your foreach loop, create a SQL Query against your data, as follows:

    string sqlB = @"SELECT f.id, f.description, f.manufacturerName, f.servings FROM foods f WHERE IS_DEFINED(f.manufacturerName)";
    
  3. Add the following line of code after the definition of sqlB to create your next item query:

    FeedIterator<Food> queryB = container.GetItemQueryIterator<Food>(sqlB, requestOptions: new QueryRequestOptions{MaxConcurrency = 5, MaxItemCount = 100});
    

    Take note of the differences in this call to GetItemQueryIterator as compared to the previous section. maxConcurrency is set to 5 and we are limiting the MaxItemCount to 100 items. This will result in paging if there are more than 100 items that match the query.

  4. Add the following lines of code to page through the results of this query using a while loop.

    int pageCount = 0;
    while (queryB.HasMoreResults)
    {
        Console.Out.WriteLine($"---Page #{++pageCount:0000}---");
        foreach (var food in await queryB.ReadNextAsync())
        {
            Console.Out.WriteLine($"\t[{food.Id}]\t{food.Description,-20}\t{food.ManufacturerName,-40}");
        }
    }
    
  5. Save all of your open tabs in Visual Studio Code

  6. Return to your terminal pane

    If you’ve closed the terminal right-click the Explorer pane and select the Open in Terminal menu option.

  7. In the open terminal pane, enter and execute the following command:

    dotnet run
    
  8. You should see a number of new results, each separated by the a line indicating the page, as follows:

        [19067] Candies, TWIZZLERS CHERRY BITES Hershey Food Corp.
    ---Page #0016---
        [19065] Candies, ALMOND JOY Candy Bar   Hershey Food Corp.
    

Note that the results are coming from multiple partitions.

If this is your final lab, follow the steps in Removing Lab Assets to remove all lab resources.