Zum Inhalt springen
DokumentationAspire ausprobieren
DokumentationAusprobieren

Connect Aspire apps to LavinMQ

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

⭐ 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.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const lavinmq: IResourceWithConnectionString
lavinmq
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addConnectionString(name: string, options?: {
environmentVariableNameOrExpression?: ReferenceExpression;
}): IResourceWithConnectionString (+1 overload)

Adds a connection string resource

addConnectionString
('lavinmq');
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
('worker', './worker', 'index.js')
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const lavinmq: IResourceWithConnectionString
lavinmq
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

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.