.NET Aspire RabbitMQ component
In this article, you learn how to use the .NET Aspire RabbitMQ client message-broker. The Aspire.RabbitMQ.Client
library is used to register an IConnection in the dependency injection (DI) container for connecting to a RabbitMQ server. It enables corresponding health check, logging and telemetry.
Get started
To get started with the .NET Aspire RabbitMQ component, install the Aspire.RabbitMQ.Client NuGet package.
dotnet add package Aspire.RabbitMQ.Client
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Example usage
In the Program.cs file of your component-consuming project, call the AddRabbitMQClient extension method to register an IConnection
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddRabbitMQClient("messaging");
You can then retrieve the IConnection
instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(IConnection connection)
{
// Use connection...
}
App host usage
To model the RabbitMQ resource in the app host, install the Aspire.Hosting.RabbitMQ NuGet package.
dotnet add package Aspire.Hosting.RabbitMQ
In your app host project, register a RabbitMQ server and consume the connection using the following methods, such as AddRabbitMQ:
var builder = DistributedApplication.CreateBuilder(args);
var messaging = builder.AddRabbitMQ("messaging");
builder.AddProject<Projects.ExampleProject>()
.WithReference(messaging);
The WithReference method configures a connection in the ExampleProject
project named messaging
.
When you want to explicitly provide the username and password, you can provide those as parameters. Consider the following alternative example:
var username = builder.AddParameter("username", secret: true);
var password = builder.AddParameter("password", secret: true);
var messaging = builder.AddRabbitMQ("messaging", username, password);
// Service consumption
builder.AddProject<Projects.ExampleProject>()
.WithReference(messaging);
For more information, see External parameters.
Configuration
The .NET Aspire RabbitMQ component provides multiple options to configure the connection based on the requirements and conventions of your project.
Use a connection string
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddRabbitMQClient
:
builder.AddRabbitMQClient("RabbitMQConnection");
And then the connection string will be retrieved from the ConnectionStrings
configuration section:
{
"ConnectionStrings": {
"RabbitMQConnection": "amqp://username:password@localhost:5672"
}
}
For more information on how to format this connection string, see the RabbitMQ URI specification docs.
Use configuration providers
The .NET Aspire RabbitMQ component supports Microsoft.Extensions.Configuration. It loads the RabbitMQClientSettings
from configuration by using the Aspire:RabbitMQ:Client
key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"RabbitMQ": {
"Client": {
"DisableHealthChecks": true
}
}
}
}
Use inline delegates
Also you can pass the Action<RabbitMQClientSettings> configureSettings
delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddRabbitMQClient(
"messaging",
static settings => settings.DisableHealthChecks = true);
You can also set up the IConnectionFactory using the Action<IConnectionFactory> configureConnectionFactory
delegate parameter of the AddRabbitMQClient
method. For example to set the client provided name for connections:
builder.AddRabbitMQClient(
"messaging",
static configureConnectionFactory:
factory => factory.ClientProvidedName = "MyApp");
Health checks
By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.
The .NET Aspire RabbitMQ component handles the following:
- Adds the health check when RabbitMQClientSettings.DisableHealthChecks is
true
, which attempts to connect to and create a channel on the RabbitMQ server. - Integrates with the
/health
HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
Observability and telemetry
.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
The .NET Aspire RabbitMQ component uses the following log categories:
RabbitMQ.Client
Tracing
The .NET Aspire RabbitMQ component will emit the following tracing activities using OpenTelemetry:
- "Aspire.RabbitMQ.Client"
Metrics
The .NET Aspire RabbitMQ component currently doesn't support metrics by default. If that changes in the future, this section will be updated to reflect those changes.
See also
.NET Aspire
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for