Search code examples
c#.netrabbitmqdotnet-aspire

How do I get the RabbitMQ connectionString in Aspire.NET setup during bootstraping


I'm using .NET Aspire 9.0. Application host setup is like:

var builder = DistributedApplication.CreateBuilder(args);

var postgres = builder.AddPostgres("Postgres-AzureEntraEditor")
        .WithDataVolume(isReadOnly: false)
        .WithLifetime(ContainerLifetime.Persistent)
        .WithPgAdmin();

var postgresdb = postgres.AddDatabase("postgres");

var rabbitmq = builder.AddRabbitMQ("messaging").WithManagementPlugin();

var apiService = builder.AddProject<Projects.Test_ApiService>("apiservice")
    .WithReference(postgresdb)
    .WithReference(rabbitmq);

builder.Build().Run();

In my "apiService" project I need to use/setup RabbitMQ. How do I get the connectionString created by aspire for RabbitMQ before var app = builder.Build();?


Solution

  • I found an extension for IHostApplicationBuilder

        string connectionString = "";
    
        if (builder.Environment.IsDevelopment())
        {
            builder.AddRabbitMQClient("messaging", (fa) =>
            {
                fa.DisableHealthChecks = true;
                fa.DisableTracing = true;
                connectionString = fa.ConnectionString;
            });
        }