Passing Aspire-Configured RabbitMQ Credentials to Environment Variables #6986
-
I have a
For working with RabbitMQ, I have a wrapper around EasyNetQ. In the DI of this wrapper service, I pass a strongly-typed object whose values are taken from the appsettings.json file:
Now I have configured RabbitMQ in the Aspire project:
How can I pass the username and password configured by Aspire for RabbitMQ into environment variables for the Worker project in the Currently, my Aspire code works, and it starts the rabbitmq container and the Worker service, but only if I comment out the service registration for working with RabbitMQ in the Worker. Otherwise, the application fails to connect to RabbitMQ. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
When you use WithReference, we pass connection information to the target resource (a project in your example). Each of the integration has the documented connection format for each resource. For RabbitMQ: On top of that, each resource has an object model with various properties: Those properties are used to create the default connection string that is used by WithReference, e.g For RabbitMQServerResource (which is the resource type when you call AddRabbitMQ): Given that you should be able to something like this in your app host project: var rabbitMq = builder.AddRabbitMQ("messaging")
.WithImageTag("4.0.5-management")
.WithManagementPlugin();
var workerProject = builder.AddProject<Worker>("worker")
.WithEnvironment(context =>
{
context.EnvironmentVariables["RabbitMQConfig__username"] = rabbitMq.Resource.UserNameParameter ?? (object)"guest";
context.EnvironmentVariables["RabbitMQConfig__password"] = rabbitMq.Resource.PasswordParameter;
context.EnvironmentVariables["RabbitMQConfig__host"] = rabbitMq.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
})
.WaitFor(rabbitMq);
builder.Build().Run(); |
Beta Was this translation helpful? Give feedback.
When you use WithReference, we pass connection information to the target resource (a project in your example). Each of the integration has the documented connection format for each resource. For RabbitMQ:
https://learn.microsoft.com/en-us/dotnet/aspire/messaging/rabbitmq-integration?tabs=dotnet-cli#use-a-connection-string
On top of that, each resource has an object model with various properties:
https://learn.microsoft.com/en-us/dotnet/api/aspire.hosting.applicationmodel.rabbitmqserverresource?view=dotnet-aspire-8.0#properties
Those properties are used to create the default connection string that is used by WithReference, e.g For RabbitMQServerResource (which is the resource type when you…