In this article, let’s create Azure Event Grid resources using Azure CLI, register Event Grid resource provider and then configure topic and endpoint. Build a .NET console application using .NET client library to send custom events to Event Grid topic. Validate that event is successfully routed to the endpoint in web application.
Following tasks are performed in this exercise:
Create Azure Event Grid resources using Azure CLI
Register Event Grid resource provider
Configure Topic and Endpoint
Send event using .NET Client Library
Sign in to Azure and Run application
Clean up resources
Create Azure Event Grid resources using Azure CLI
First basic task is to create and make ready Azure Event Grid resources to Azure using Azure CLI. Azure CLI (Azure Cloud Shell) is used to create the required resources. Azure CLI is the free Bash shell which can be run directly within the Azure portal. That is preinstalled and configured within the account. Perform the below steps one by one to achieve this.
Use Azure CLI (Azure Cloud Shell)
It is a free Bash shell which can be run directly within the Azure portal. It is preinstalled and configured within an account.
Click Cloud Shell button on the menu bar at the top right side of the Azure portal.

It will launch an interactive shell which can be used to run the steps outlined in this article. Select a Bash environment. To use code editor in cloud shell toolbar go to Settings menu and then select Go to Classic version.

Create resource group - Resource group is a logical container used to hold related resources. In it include the resources which you want to manage as a group. App configuration resource belongs to this resource group. Create it using az group create command.
az group create \
--name myResourceGroup1 \
--location eastus2 eastus2 is location code instead of it one can use their nearest region location.
Create variables - Here, several commands require unique names/values with same parameters. Hence, for easy to manege lets create some variables, assign values and reuse them in the command parameters. let command is used as below to create required variables.
let rNum=$RANDOM
resourceGroup=myResourceGroup1
location=eastus2
topicName="mytopic-evgtopic-${rNum}"
siteName="evgsite-${rNum}"
siteURL="https://${siteName}.azurewebsites.net"Note: Open siteURL’s value in new tab browser to ensure that the web application is running. Leave the browser as running and that is used to show any updates. Click on eye icon option to expand event data. Event Grid sends validation events to endpoint to verify the received event data.
Register Event Grid resource provider
Azure Resource Provider is a service which defines and manages specific type resources in Azure. Microsoft.EventGrid namespace is required to registered for the application. az provider register command is used to register namespaces as like below. This command will take few minutes to complete the registration process.
az provider register \
--namespace Microsoft.EventGrid Configure Topic and Endpoint
In this section, lets create topic in event grid and create a message endpoint, and then subscribe to topic. One by one perform the following three steps to achieve this.
Create Topic in Event Grid
az eventgrid topic create command is used to create a topic and it will be used in .NET code later on in Event Grid. Name must be a unique name as it is a part of DNS entry.
az eventgrid topic create \
--name $topicName \
--location $location \
--resource-group $resourceGroupCreate Message Endpoint
Now, create an endpoint for event message. Endpoint is used to take actions based on the event data. az deployment group create command is used to create a deployment at resource group from the remote template file. Deployed solution contains App Service plan, App Service web application and source code from the GitHub repository.
az deployment group create \
--name myDeploymentGroup1 \
--resource-group $resourceGroup \
--template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/main/azuredeploy.json" \
--parameters siteName=$siteName hostingPlanName=viewerhostSubscribe to Topic
Once, message endpoint is created and ready to use next one task is to subscribe to the topic. az eventgrid event-subscription create command is used to subscribe to a topic specified. Subscribe to Event Grid topic is used to tell Event Grid which events to be tracked and where to send such events. Following script retrieves subscription ID from the account and used in next command to create the event subscription.
let endpoint="${siteURL}/api/updates" \
topicId=$(az eventgrid topic show --resource-group $resourceGroup \
--name $topicName --query "id" --output tsv)
az eventgrid event-subscription create \
--source-resource-id $topicId \
--name TopicSubscription \
--endpoint $endpointSend event using .NET Client Library
Next step is to set-up the .NET console application to send an event by using .NET Client Library. Perform the following steps one by one using Cloud Shell to achieve this.
.NET Project Setup
Set up one console application to route events to custom endpoint with Azure Event Grid using .NET Client Library.
Create project directory and change to that directory. First create a directory for the project using below command and move to that directory.
mkdir eventgrid1
cd eventgrid1Create .NET console application to add and fetch secrets through it. dotnet new console command is used to create a .NET console app.
dotnet new console
Install Packages - run dotnet add package command to add Azure.Messaging.EventGrid and dotenv.net packages in project to use the SDK. Azure.Messaging.EventGrid namespace is used to building scalable event-driven applications, provides logical container (namespace) for the related Event Grid resources such as topics, clients and enhance complex event routing scenarios. dotenv.net is C# library for .NET apps. It is used to load environment variables from an .env files.

Join the conversation! Your thoughts help the community grow.