Azure Service Bus Quick Start
This tutorial will get you from zero to up and running with Azure Service Bus and MassTransit.
Prerequisites
The following instructions assume you are starting from a completed In-Memory Quick Start
This example requires the following:
- a functioning installation of the dotnet runtime and sdk (at least 6.0)
- an Azure account, where you have administrative control
Setup Azure Service Bus
To continue from this point, you must have a valid Azure subscription with an Azure Service Bus namespace. A shared access policy with Manage permissions is required to use MassTransit with Azure Service Bus.
- Navigate to Service Bus
- Create a namespace
- Pricing Tier: This must be Standard or Premium
- Create a Shared access policy
- Make sure to grant
Manage
- The
Primary Connection String
will be used for the rest of the steps.
- Make sure to grant
Change the Transport to Azure Service Bus
Add the MassTransit.Azure.ServiceBus.Core package to the project.
$ dotnet add package MassTransit.Azure.ServiceBus.Core
Edit Program.cs
Change UsingInMemory
to UsingAzureServiceBus
.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddMassTransit(x =>
{
// elided ...
x.UsingAzureServiceBus((context,cfg) =>
{
cfg.Host("your connection string");
cfg.ConfigureEndpoints(context);
});
});
services.AddHostedService<Worker>();
});
Run the project
$ dotnet run
The output should have changed to show the message consumer generating the output (again, press Control+C to exit). Notice that the bus address now starts with sb
.
Building...
info: MassTransit[0]
Configured endpoint Message, Consumer: GettingStarted.MessageConsumer
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/chris/Garbage/start/GettingStarted
info: MassTransit[0]
Bus started: sb://your-service-bus-namespace/
info: GettingStarted.MessageConsumer[0]
Received Text: The time is 3/24/2021 12:11:10 PM -05:00
At this point, the service is connecting to Azure Service Bus and publishing messages which are received by the consumer.
๐