← Return To Home

Getting started with Azure Service Bus with .NET Core

In this post, you will learn more about Azure Service Bus with .NET Core. .NET Core is a general-purpose development platform supported by Microsoft and the .NET community on GitHub. It is a cross-platform which supports Windows, macOS, and Linux, and can be used in device, cloud, and embedded/IoT scenarios. Microsoft Azure Service Bus provides a reliable information delivery service. The main purpose of this service is to make communication easier and when two or more parties want to exchange information, they need a communication facilitator.

Prerequisites

  • .NET Core
  • An Azure subscription.
  • A Service Bus namespace
  • A Service Bus Queue

Free download this blog as a PDF document for offline read.

Create a Service Bus Namespace and Queue in the portal

Log into the Azure portal. Click “+NEW” and choose “Enterprise Integration”. Select “Service Bus”.

New Azure Service Bus

In the ‘Create Namespace’ blade enter the details for the creation of service bus namespace.

  1. Name: Unique namespace for Service Bus
  2. Pricing Tier: Select ‘Standard’
  3. Subscription: Select the appropriate subscription from the list.
  4. Resource Group: Select ‘Use Existing’ and enter the ‘Resource group’ name.
  5. Location: Select the nearest location. Click Create.

Service Account Blade

Once the ‘Service Bus deployment successful’ notification is received, Click on ‘Service Bus namespace’ object.

Service Bus Dashboard

Click on ‘Add Queue’. On ‘Create queue’ blade enter the queue name. Select default values and click on ‘Create’.

Add New Queue

 Once the queue is created, it should be visible in the ‘Service Bus namespace’ blade.

Created New Queue

Click on queue ‘Orders’. In the Setting pane for queue ‘Orders’, click on ‘Shared access policies’.

Shared Access Polices

Click on ‘Add’ in ‘Shared access policies’ blade. Enter Policy name ‘write’ and select the claim ‘Send

Policy Name

Repeat the previous step to create policy ‘read’ with claim ‘Listen’.

Queue Read

Both policies i.e. ‘read’ and ‘write’ should be visible in ‘Shared access policies’ blade.

Write And Read

Click on copy and note the Primary key associated with Shared access policies ‘read’ and ‘write’.

Write Key

Read Key

Create a new .NET Core project

Now we need to create two new projects to send and receive messages.

  • SendSample
  • ReceiveSample

SendSample Project

In Visual Studio, click on File / New / Project and select the project template Visual C# / .NET Core / Console App (.NET Core).

New .NET Core Project

Add the Service Bus NuGet package

In Solution Explorer, in the Project Name, right-click References, then click Manage NuGet Packages. Click the Browse tab, then search for Microsoft.Azure.ServiceBus. Select Microsoft.Azure.ServiceBus package, click Install and accept the terms of use.

NuGet-Package

Add the following using statement to the top of the Program.cs file.

Add the following code to the Program Class. Set the ServiceBusConnectionString variable to the connection string that was obtained when creating the namespace, and set QueueName to the queue name that you used when creating the queue.

Microsoft Ignite 2019

ReceiveSample Project

To receive the messages you just sent, create a ReceiveSample Project and add a reference to the Service Bus NuGet package, similar to the previous sender Project.

Add the following using statement to the top of the Program.cs file.

Add the following code to the Program Class. Set the ServiceBusConnectionString variable to the connection string that was obtained when creating the namespace, and set QueueName to the queue name that you used when creating the queue.

Solution-Explorer

In the Solution Explorer window, right-click the solution and choose Properties. In the property page that comes up, click on Startup Project and then choose Multiple startup projects: and change the Action for both queue projects to “Start”. Then click OK.

 Multiple Project

In Visual Studio, click the Start button on the menu bar.

Run Program

Now we can see two running console windows that should be showing some output.

Output

Conclusion

To end with a quick recap. this blog helps you to send and receive messages through Queues using .NET Core. Also, you can use Azure Service Bus on multiple platforms like Windows, macOS, and Linux with ease!