In order to run the sample in this directory, replace the following bracketed values in the Program.cs
file.
private const string ServiceBusConnectionString = "{Service Bus connection string}";
private const string QueueName = "{Queue path/name}";
Once you replace the above values run the following from a command prompt:
dotnet restore
dotnet build
dotnet run
For further information on how to create this sample on your own, follow the rest of the tutorial.
In this tutorial, we will write a console application to receive messages from a Service Bus queue.
- .NET Core
- An Azure subscription.
- A Service Bus namespace
- A Service Bus queue
- Create a new .NET Core application. Check out this link with help to create a new application on your operating system.
-
Add the following to your project.json, making sure that the solution references the
Microsoft.Azure.ServiceBus
project."Microsoft.Azure.ServiceBus": { "target": "project" }
-
Add the following using statement to the top of the Program.cs file.
using Microsoft.Azure.ServiceBus;
-
Add the following private variables to the
Program
class, and replace the placeholder values:private static QueueClient queueClient; private const string ServiceBusConnectionString = "{Service Bus connection string}"; private const string QueueName = "{Queue path/name}";
-
Create a new method called
ReceiveMessages
with the following code:// Receives messages from the queue in a loop private static async Task ReceiveMessages() { Console.WriteLine("Press ctrl-c to exit receive loop."); while (true) { try { // Receive the next message from the queue var message = await queueClient.ReceiveAsync(); // Write the message body to the console Console.WriteLine($"Received message: {message.GetBody<string>()}"); // Complete the messgage so that it is not received again await message.CompleteAsync(); } catch (Exception exception) { Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}"); } // Delay by 10 milliseconds so that the console can keep up await Task.Delay(10); } }
-
Create a new method called
MainAsync
with the following code:private static async Task MainAsync(string[] args) { // Creates a ServiceBusConnectionStringBuilder object from the connection string, and sets the EntityPath. var connectionStringBuilder = new ServiceBusConnectionStringBuilder(ServiceBusConnectionString) { EntityPath = QueueName }; // Initializes the static QueueClient variable that will be used in the ReceiveMessages method. queueClient = QueueClient.CreateFromConnectionString(connectionStringBuilder.ToString()); await ReceiveMessages(); // Close the client after the ReceiveMessages method has exited. await queueClient.CloseAsync(); }
-
Add the following code to the
Main
method:MainAsync(args).GetAwaiter().GetResult();
-
Run the program, and check the Azure portal. Click the name of your queue in the namespace Overview blade. Notice that the Active message count value should now be 0.
Congratulations! You have now received messages from a Service Bus queue, using .NET Core.