Skip to main content

Using Azure Cosmos DB with C#

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Intermediate

In this lab, you will gain experience using C# and Visual Studio to connect to and use Cosmos DB. You will RDP into a Windows VM set up with Visual Studio and update a pre-built solution with the appropriate Cosmos DB C# code to connect to Cosmos DB, upload documents, and use the SQL API to query documents. When this is complete, you will run a console application to see everything working and can go back to the Azure portal to verify the documents were saved. Upon completion of the lab, you will have gained the experience required to work with Cosmos DB using C#.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Azure Cosmos DB with C#

Introduction

In this lab, you will gain experience using C# and Visual Studio to connect to and use Cosmos DB. You will RDP into a Windows VM set up with Visual Studio and update a pre-built solution with the appropriate Cosmos DB C# code to connect to Cosmos DB, upload documents, and use the SQL API to query documents. When this is complete, you will run a console application to see everything working and can go back to the Azure portal to verify the documents were saved. Upon completion of the lab, you will have gained the experience required to work with Cosmos DB using C#.

Solution

Log in to the Azure Portal using the credentials provided on the lab instructions page.

RDP into the VM and Open the Visual Studio Solution

  1. Click All resources in the left-hand menu.

  2. Click the listed lab-VM virtual machine.

  3. Click Connect.

  4. Click Download RDP File.

  5. Select Save File and click OK.

  6. Open your downloads folder, and choose to view the lab-VM.rdp file in the folder view.

  7. Right-click the file and choose Edit.

  8. In the Display tab, change the resolution from full-screen to something smaller (e.g., 1024x768).

  9. In the Local Resources tab, in the Keyboard section, change the dropdown to On the remote computer.

  10. Click Connect.

  11. Open the file in your Remote Desktop app.

  12. Log in to the virtual machine using the following credentials:

    • User Name: azureuser
    • Password: LA!2018!Lab
  13. Open PowerShell.

  14. Paste in the following script (it will download to our remote desktop the solution zip file, extract its contents, and then delete the zip file):

    Add-Type -Path "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.IO.Compression.FileSystem.dll"
    
    $url = "https://github.com/linuxacademy/content-azure-labs/blob/master/zips/Azure-LearningActivity-Cosmos DB.zip?raw=true"
    $zipfile = "C:UsersazureuserDesktopAzure-LearningActivity-Cosmos DB.zip"
    $folder = "C:UsersazureuserDesktop"
    
    Invoke-WebRequest -UseBasicParsing -OutFile $zipfile $url
    
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $folder)
    
    Remove-Item -Path $zipfile
  15. Close out of PowerShell.

  16. Click the newly created folder, containing the solution, on the desktop.

  17. Click to open the Azure-LearningActivity-Cosmos DB file in Visual Studio.

  18. Click Sign in in the dialog.

  19. Use the same lab-provided credentials you used to sign in to the Azure portal.

  20. Choose a theme, and click Start Visual Studio.

Update the Visual Studio Solution and Run the Console App

Create a New Container in the Azure Portal

  1. In the Azure portal, click All resources.
  2. Select the Azure Cosmos DB account resource in the list.
  3. Click Add Container.
  4. In the Add Container pane, set the following values:
    • Database id: Create new, and name it db1
    • Container id: collection1
    • Partition key: /Partition
    • Throughput: 400
  5. Click OK.

Update the Visual Studio Code on VM

  1. Back on the virtual machine, in the Visual Studio window, double-click Program.cs in the right-hand menu to open it.
  2. In the Program class, we need to enter a couple pieces of missing information: EndpointUrl and PrimaryKey. In the Azure Portal, head to the Overview section for our Cosmos DB account.
  3. Copy the listed URI.
  4. Back in Visual Studio, paste it in as the EndpointUrl value.
  5. In the Azure portal, scroll down and click Keys (under the Settings heading).
  6. Copy the listed primary key.
  7. Back in Visual Studio, paste it in as the PrimaryKey value.
  8. Click the disk icon to save the code.
  9. Click the play button icon to run the console app.
  10. Back in the Azure portal, scroll down and click Document Explorer (under the Containers heading).
  11. Click Open Data Explorer.
  12. Click collection1.
  13. Click Documents. We should see two items listed, which means we were successful.

Conclusion

Congratulations on successfully completing this hands-on lab!