Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Includes:
Hosting integration only —
Client integration not included
Note
This integration is part of the Aspire Community Toolkit and isn't officially supported by the Aspire team.
LavinMQ is a reliable messaging and streaming broker, which is easy to deploy on cloud environments, on-premises, and on your local machine. The Aspire LavinMQ integration enables you to create new container instances from .NET with the docker.io/cloudamqp/lavinmq container image.
LavinMQ is a message broker built on Crystal. LavinMQ implements the AMQP protocol. Being a message queue software, also called a message broker, messages are published by a sending service called a producer, via the broker, to then be consumed by the receiving service called a consumer. Essentially, like a postal service, LavinMQ gives an organized, safe place for messages to wait until another application or part of the system can come along and consume them for processing. It is known to be a wire compatible messaging server with RabbitMQ, which means that you can use the RabbitMQ client to interact with LavinMQ.
Hosting integration
The LavinMQ hosting integration models a LavinMQ server as the Aspire.Hosting.ApplicationModel.LavinMQContainerResource type. To access this type and its APIs add the 📦 CommunityToolkit.Aspire.Hosting.LavinMQ NuGet package in the app host project.
dotnet add package CommunityToolkit.Aspire.Hosting.LavinMQ
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Add LavinMQ server resource
In your app host project, call AddLavinMQ on the builder instance to add a LavinMQ container resource:
var builder = DistributedApplication.CreateBuilder(args);
var lavinMq = builder.AddLavinMQ("messaging");
builder.AddProject<Projects.ExampleProject>()
.WithReference(lavinMq);
// After adding all resources, run the app...
When Aspire adds a container image to the app host, as shown in the preceding example with the docker.io/cloudamqp/lavinmq image, it creates a new LavinMQ server instance on your local machine. A reference to your LavinMQ server (the lavinMq variable) is added to the ExampleProject. The LavinMQ server resource includes default credentials with a username of "guest" and a password of "guest".
The WithReference method configures a connection in the ExampleProject named "messaging". For more information, see Built-in resource types.
Tip
If you'd rather connect to an existing LavinMQ server, call AddConnectionString instead. For more information, see Reference existing resources.
LavinMQ container resource management plugin
LavinMQ includes a management interface by default that provides a web-based interface for monitoring and managing the message broker.
Add LavinMQ server resource with data volume
To add a data volume to the LavinMQ container resource, call the Aspire.Hosting.LavinMQBuilderExtensions.WithDataVolume method on the LavinMQ container resource:
var builder = DistributedApplication.CreateBuilder(args);
var lavinMq = builder.AddLavinMQ("messaging")
.WithDataVolume(isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(lavinMq);
// After adding all resources, run the app...
The data volume is used to persist the LavinMQ server data outside the lifecycle of its container. The data volume is mounted at the /var/lib/lavinmq path in the LavinMQ server container and when a name parameter isn't provided, the name is generated at random. For more information on data volumes and details on why they're preferred over bind mounts, see Docker docs: Volumes.
Add LavinMQ server resource with data bind mount
To add a data bind mount to the LavinMQ container resource, call the Aspire.Hosting.LavinMQBuilderExtensions.WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var lavinMq = builder.AddLavinMQ("messaging")
.WithDataBindMount(
source: @"C:\LavinMQ\Data",
isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(lavinMq);
// After adding all resources, run the app...
Important
Data bind mounts have limited functionality compared to volumes, which offer better performance, portability, and security, making them more suitable for production environments. However, bind mounts allow direct access and modification of files on the host system, ideal for development and testing where real-time changes are needed.
Data bind mounts rely on the host machine's filesystem to persist the LavinMQ server data across container restarts. The data bind mount is mounted at the C:\LavinMQ\Data on Windows (or /LavinMQ/Data on Unix) path on the host machine in the LavinMQ server container. For more information on data bind mounts, see Docker docs: Bind mounts.
Hosting integration health checks
The LavinMQ hosting integration automatically adds a health check for the LavinMQ server resource. The health check verifies that the LavinMQ server is running and that a connection can be established to it.
Because LavinMQ is wire compatible with AMQP 0.9.1, we can leverage the existing RabbitMQ health check integration.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.RabbitMQ NuGet package.
Client integration
LavinMQ is wire compatible with RabbitMQ. This may introduce a bit of confusion at first, but think of LavinMQ as a RabbitMQ server with a different name in terms of the .NET client integration. You can see an example of using the explicit RabbitMQ.Client in the LavinMQ .NET sample code documentation. To get started with the Aspire RabbitMQ client integration, install the 📦 Aspire.RabbitMQ.Client NuGet package in the client-consuming project, that is, the project for the application that uses the LavinMQ client. The LavinMQ client integration registers an IConnection instance that you can use to interact with LavinMQ.
dotnet add package Aspire.RabbitMQ.Client
Add LavinMQ/RabbitMQ client
In the Program.cs file of your client-consuming project, call the AddRabbitMQClient extension method on any IHostApplicationBuilder to register an IConnection for use via the dependency injection container. The method takes a connection name parameter.
builder.AddRabbitMQClient(connectionName: "messaging");
Tip
The connectionName parameter must match the name used when adding the LavinMQ container resource in the app host project. For more information, see Add LavinMQ server resource.
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...
}
For more information on dependency injection, see .NET dependency injection.
Add keyed RabbitMQ client
There might be situations where you want to register multiple IConnection instances with different connection names. To register keyed LavinMQ clients, call the AddKeyedRabbitMQClient method:
builder.AddKeyedRabbitMQClient(name: "chat");
builder.AddKeyedRabbitMQClient(name: "queue");
Then you can retrieve the IConnection instances using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(
[FromKeyedServices("chat")] IConnection chatConnection,
[FromKeyedServices("queue")] IConnection queueConnection)
{
// Use connections...
}
For more information on keyed services, see .NET dependency injection: Keyed services.
Configuration
The Aspire RabbitMQ integration 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 the AddRabbitMQClient method:
builder.AddRabbitMQClient(connectionName: "messaging");
Then the connection string is retrieved from the ConnectionStrings configuration section:
{
"ConnectionStrings": {
"messaging": "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 Aspire LavinMQ integration supports Microsoft.Extensions.Configuration. It loads the RabbitMQClientSettings from configuration by using the Aspire:RabbitMQ:Client key. The following snippet is an example of a appsettings.json file that configures some of the options:
{
"Aspire": {
"RabbitMQ": {
"Client": {
"ConnectionString": "amqp://username:password@localhost:5672",
"DisableHealthChecks": true,
"DisableTracing": true,
"MaxConnectRetryCount": 2
}
}
}
}
For the complete RabbitMQ client integration JSON schema, see Aspire.RabbitMQ.Client/ConfigurationSchema.json.
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",
configureConnectionFactory:
static factory => factory.ClientProvidedName = "MyApp");
Client integration health checks
By default, Aspire integrations enable health checks for all services. For more information, see Aspire integrations overview.
The Aspire RabbitMQ integration:
- Adds the health check when RabbitMQClientSettings.DisableHealthChecks is
false, which attempts to connect to and create a channel on the LavinMQ server. - Integrates with the
/healthHTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
Observability and telemetry
Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see Aspire integrations overview. Depending on the backing service, some integrations might only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
The Aspire RabbitMQ integration uses the following log categories:
RabbitMQ.Client
Tracing
The Aspire RabbitMQ integration emits the following tracing activities using OpenTelemetry:
Aspire.RabbitMQ.Client
Metrics
The Aspire RabbitMQ integration currently doesn't support metrics by default.