跳转到内容
DocsTry Aspire
DocsTry

Connect Aspire apps to LavinMQ

此内容尚不支持你的语言。

⭐ Community Toolkit LavinMQ logo

This page describes how consuming apps connect to a LavinMQ resource. For broker ports, storage, management, and health checks, see Set up LavinMQ in the Aspire AppHost.

LavinMQ implements AMQP 0-9-1 and is wire-compatible with RabbitMQ. There is no LavinMQ-specific Community Toolkit client package; use a RabbitMQ-compatible client.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. A resource named lavinmq provides:

PropertyEnvironment variableDescription
HostLAVINMQ_HOSTBroker hostname or IP address
PortLAVINMQ_PORTAMQP endpoint port
UsernameLAVINMQ_USERNAMEUsername; defaults to guest
PasswordLAVINMQ_PASSWORDPassword; defaults to guest
UriLAVINMQ_URIamqp://{Username}:{Password}@{Host}:{Port}/

Unlike the Aspire RabbitMQ resource, the LavinMQ resource doesn’t expose a VirtualHost property. Its URI uses /.

For an external broker, model the connection string in the AppHost instead of adding a LavinMQ container:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var lavinmq = builder.AddConnectionString("lavinmq");
builder.AddProject<Projects.Worker>("worker")
.WithReference(lavinmq);
builder.Build().Run();

C# configuration reads ConnectionStrings:lavinmq; environment-variable consumers read ConnectionStrings__lavinmq. The individual LavinMQ properties aren’t injected for an external connection-string resource.

C# apps can use the Aspire RabbitMQ client integration because LavinMQ is wire-compatible with RabbitMQ. Install:

.NET CLI — Add Aspire.RabbitMQ.Client package
dotnet add package Aspire.RabbitMQ.Client

Register the client with the LavinMQ resource name:

Program.cs
builder.AddRabbitMQClient(connectionName: "lavinmq");

Then resolve RabbitMQ.Client.IConnection through dependency injection.

To use the URI directly with the RabbitMQ .NET client:

Program.cs
using RabbitMQ.Client;
var uri = Environment.GetEnvironmentVariable("LAVINMQ_URI")
?? throw new InvalidOperationException("LAVINMQ_URI is not set.");
var factory = new ConnectionFactory { Uri = new Uri(uri) };
using var connection = await factory.CreateConnectionAsync();
using var channel = await connection.CreateChannelAsync();

Aspire.RabbitMQ.Client adds a connection health check and RabbitMQ client tracing for C# apps. Other language clients provide their own logging, recovery, and instrumentation options.