In this article, let’s create Azure Service Bus resources, Azure Service Bus namespace and Queue using Azure CLI. Assign “Azure Service Bus Data Owner” role to Microsoft Entra (Azure AD) user name. Build a .NET console application using Azure.Messaging.ServiceBus SDK to send and receive the messages. Sign in to Azure and run the application. After this exercise, you will be able to send and receive messages from Azure Service Bus using .NET Client Library.
Following tasks are performed in this article:
Create Azure Service Bus resources.
Create Azure Service Bus namespace and queue.
Assign role to Microsoft Entra (Azure AD) username.
Send and Receive Messages using .NET Client Library.
Sign in to Azure and run Application.
Clean up resources.
Create Azure Service Bus resources
First basic task is to create and make ready Azure Service Bus 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 resourceGroup=myResourceGroup1 \
location=eastus2 \
namespaceName=myServiceBusNameSpace$RANDOMCreate Azure Service Bus namespace and Queue
In this section, lets create a Service Bus namespace, and then create a Queue. One by one perform the following steps in Azure CLI to achieve this.
Create Azure Service Bus namespace
az servicebus namespace create command is used to create an Azure Service Bus namespace and it will be used in .NET console application. This namespace is the logical container for service bus resources in Azure. It provides a unique container which allows to create one or more service bus.
az servicebus namespace create \
--name $namespaceName \
--location $location \
--resource-group $resourceGroupCreate Queue
Below az servicebus queue create command is used to create a Queue with name myQueue inside above created Service Bus namespace. This queue will be used to hold the messages.
az servicebus queue create \
--name myQueue \
--resource-group $resourceGroup \
--namespace-name $namespaceNameAssign role to Microsoft Entra (Azure AD) user
Let’s assign Microsoft Entra user to “Azure Service Bus Data Owner” role at the Service Bus namespace level to allow console application to send and receive messages. It is important to assign roles to Microsoft Entra (Azure AD) user name. 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 Service Bus 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 Service Bus 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 servicebus namespace show \
--resource-group $resourceGroup \
--name $namespaceName\
--query id \
--output tsv)Create and assign “Azure Service Bus Data Owner” role
Finally, create and assign “Azure Service Bus Data Owner” role using az role assignment create command. Command execution will create and assign “Azure Service Bus Data Owner” role which gives permission to send and receive the messages. 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 receive actual value at command execution time.
az role assignment create \
--assignee $userPrincipal\
--role "Azure Service Bus Data Owner" \
--scope $resourceIDNow, required basic resources are deployed and ready to use in console application.
Send and Receive Messages using .NET Client Library
Let’s set-up the .NET console application to send and receive the messages using .NET Client Library. Perform the following steps one by one in to Cloud Shell to achieve this.
.NET Project Setup
Set up one console application to send and receive message 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 servicebus1
cd servicebus1Create .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.ServiceBus packages in project to use the SDK.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.ServiceBusStarter Code
Add the below starter code into the project and replace template code in Program.cs file by using editor in Cloud Shell.
Application editing - open Program.cs file to edit it in the Azure CLI using code command.
code Program.csCode replacing - replace existing code of Program.cs with the below code. This is the initial starter code and will replace last three commented lines with actual code in next “Complete code” section. Replace MY_NAMESPACE with actual service bus namespace.
using System;
using System.Timers;
using Azure.Identity;
using Azure.Messaging.ServiceBus;
// Replace MY_NAMESPACE with actual Service Bus namespace
string serviceBusClientNameSpace = "MY_NAMESPACE.servicebus.windows.net";
string queueName = "myQueue";
// CREATE SERVICE BUS CLIENT
// SEND MESSAGES TO QUEUE
// PROCESS MESSAGES FROM QUEUE
// Dispose client
await objServiceBusClient.DisposeAsync();

Join the conversation! Your thoughts help the community grow.