In this article, let’s create Azure Event Hubs resources, Azure Event Hubs namespace and Event Hub using Azure CLI. Assign “Azure Event Hubs Data Owner” role to Microsoft Entra (Azure AD) user name. Build a .NET console application using Azure.Messaging.EventHubs SDK to send and receive the events. Sign in to Azure and run the application. After this exercise, you will be able to send and retrieve events from Azure Event Hubs using .NET Client Library.
Following tasks are performed in this article:
Create Azure Event Hub resources.
Create Azure Event Hubs namespace and Event Hub.
Assign role to Microsoft Entra (Azure AD) user.
Send and Retrieve Events using .NET Client Library.
Sign in to Azure and Run Application.
Clean up resources.
Create Azure Event Hub resources
First basic task is to create and make ready Azure Event Hub 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 manage let's create some variables, assign values and reuse them in the command parameters. let command is used as below to create required variables.
let resourceGroup=myResourceGroup1
location=eastus2
namespaceName=myEventHubsns$RANDOMCreate Azure Event Hubs namespace and Event Hub
In this section, lets create an Event Hubs namespace, and then create an Event Hub. One by one perform the following steps in Azure CLI to achieve this.
Create Azure Event Hub namespace
az eventhub namespace create command is used to create an Azure Event Hub namespace and it will be used in .NET console application. This namespace is the logical container for event hub resources in Azure. It provides a unique container which allows to create one or more event hub.
az eventhubs namespace create \
--name $namespaceName \
--location $location \
--resource-group $resourceGroupCreate Event Hub
Below az eventhubs eventhub create command is used to create Event Hub with name myEventHub inside above created Event Hubs namespace.
az eventhubs eventhub create \
--name myEventHub \
--resource-group $resourceGroup \
--namespace-name $namespaceNameAssign role to Microsoft Entra (Azure AD) user
Let’s assign Microsoft Entra user to “Azure Event Hubs Data Owner” role at the Event Hubs namespace level to allow console application to send and receive messages. It is important to assign roles to Microsoft Entra (Azure AD) username. Roles will define what access and permissions a user has within an organization’s Microsoft cloud environment. Roles determine what resources and actions a user can perform. “Azure Event Hubs Data Owner” role is to be assigned to Microsoft Entra user which allows application to send and receive messages. One by one perform below steps in Azure CLI.
Retrieve userPrincipalName
First, retrieve userPrincipalName from account as mentioned in following command. It retrieves the information about currently authenticated user including their UPN and represents that who the role will be assigned to.
userPrincipal=$(az rest
--method GET
--url https://graph.microsoft.com/v1.0/me \
--headers 'Content-Type=application/json' \
--query userPrincipalName
--output tsv)Retrieve resource ID
Second, get the resource ID of Event Hubs namespace using below command. This will be used in next step to set scope for role assignment. Resource ID sets scope for role assignment to specific namespace.
resourceID=$(az eventhubs namespace show \
--resource-group $resourceGroup \
--name $namespaceName\
--query id \
--output tsv)Create and Assign Azure Event Hubs Data Owner role
Finally, create and assign “Azure Event Hubs Data Owner” role using az role assignment create command. Command execution will create and assign “Azure Event Hubs Data Owner” role which gives permission to send and retrieve the events. This will give permissions to manage next routines. userPrincipal and resourceID variables are created in first and second steps which are used in this command to retrieve actual value at command execution time.
az role assignment create \
--assignee $userPrincipal\
--role "Azure Event Hubs Data Owner" \
--scope $resourceIDNow, required basic resources are deployed and ready to use in console application.
Send and Retrieve Events using .NET Client Library
Let’s set-up the .NET console application to send and retrieve the the events from Event Hub using .NET Client Library. Perform the following steps one by one into Cloud Shell to achieve this.
.NET Project Setup
Set up one console application to send and retrieve events 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 eventhubs1
cd eventhubs1Create .NET console application to add and fetch secrets through it. dotnet new console command is used to create a .NET console app.
dotnet new consoleInstall Packages - run dotnet add package command to add Azure.Identity and Azure.Messaging.EventHubs packages in project to use the SDK.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.EventHubs
Join the conversation! Your thoughts help the community grow.