Using Azure Search with C#

Hands-On Lab

 

Photo of Jason Calton

Jason Calton

Training Architect

Length

01:00:00

Difficulty

Intermediate

In this lab, you will gain experience using C#, the Azure Search SDK, and Visual Studio to connect to and use Azure Search. You will use RDP to connect to a Windows VM and update a pre-built Visual Studio solution with the appropriate C# code to connect to your search service, create a search index, index sample JSON data, and search your index. When you're finished, you will run the console application to see everything working. Finally, you will verify documents are indexed using the Azure Portal. Upon completion of the lab, you will have gained the experience required to work with Azure Search 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 Search with C

Introduction

In this lab, you will gain experience using C#, the Azure Search SDK, and Visual Studio to connect to and use Azure Search.

You will use RDP to connect to a Windows VM and update a pre-built Visual Studio solution with the appropriate C# code to connect to your search service. Then you'll create a search index, index sample JSON data, and search your index. When you're finished, you will run the console application to see everything working. Finally, you will verify documents are indexed using the Azure Portal.

Upon completion of the lab, you will have gained the experience required to work with Azure Search using C#.

Getting Started

Connect to the Azure Portal using the credentials provided in the Credentials section on the hands-on lab page. Click Maybe Later in the Guided Tour window if it appears.

Connect to the VM, Download Solution, and Set up Visual Studio

Connect to the VM

In the Azure Portal, click on All resources in the menu on the left. Click on lab-vm in the list. Click Connect and then click Download RDP file.

Connect using the downloaded RDP file, providing the credentials below when prompted:

UserName : "azureuser"
Password : "LA!2018!Lab"

Download Solution

Now that we are connected to our VM, let's open a PowerShell window by clicking the Windows icon in the bottom left, searching for Powershell, and clicking on Windows Powershell in the list.

Copy the following commands to your clipboard and paste them into the PowerShell window by right-clicking in the window. This will download the files we need to set up Visual Studio:

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-search.zip?raw=true"
$zipfile = "C:UsersazureuserDesktopazure-search.zip"
$folder = "C:UsersazureuserDesktop"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -UseBasicParsing -OutFile $zipfile $url

[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $folder)

Remove-Item -Path $zipfile
#

Close the PowerShell window once these commands have completed successfully.

Set up Visual Studio

Open the azure-search folder on the desktop of the VM. Double-click on the azure-search file to launch and configure Visual Studio.

When prompted, click Sign In and provide the same credentials we used to sign in to the Azure Portal earlier. Once the sign in process has completed, click Start Visual Studio.

Visual Studio will take a few minutes to load.

Update Visual Studio Solution, Run Console App, Verify Documents Indexed in Azure Portal

Update Visual Studio Solution

In the Solution Explorer window on the right in Visual Studio, double-click on the Program.cs class.

We will need to fill in a few fields with information from the Azure Portal. Let's switch back to the Azure Portal, click on All Resources at the top of the page. Locate the Azure Search service in the list. It starts with labsearch, followed by a few randomized letters, and will show as Search service in the Type column. Click on the Search service name.

Select and copy the name of the Search service to your clipboard. In the Program.cs script, replace the blank on line 13 with the Search service name we copied from the Azure Portal.

Back in the Azure Portal, click on Keys in the Settings section of our Search service. Copy the Primary Admin Key and replace the blank on line 14 in the Program.cs script in Visual Studio.

On line 60, replace the blank with Index.

On line 66, replace the blank with CreateOrUpdate.

On line 130, replace the blank with Upload.

On line 134, replace the blank with Index.

On line 170, replace the blank with Search.

On line 182, replace the blank with Search.

On line 196, replace the blank with Search.

On line 202, replace the blank with Search.

Run Console App

Save our changes by clicking the Save icon in the menu at the top of Visual Studio. Next, click the Run button next to azure-search in the menu at the top of Visual Studio. This will run our console app.

A new window will open and display a lot of information. Feel free to scroll through this window to see the Search results.

Verify Documents Indexed in Azure Portal

We can also verify the search results in the Azure Portal. In the Indexes tab for our Search service, we can note the Document Count and Storage Size fields. The Usage tab will show the same information, but in graph form.

Conclusion

Congratulations on completing this lab!