microsoft / microsoft/aspire

[RabbitMQ Hosting] Allow setting a default vhost

Open
#7,620 3 comments 2 reactions 0 assignees View on GitHub
area-integrations rabbitmq
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I am trying to set a default vhost by setting the `RABBITMQ_DEFAULT_VHOST` environment variable, but the resource's current implementation is not able to understand that when checking its health.

### Describe the solution you'd like

Allow setting the default vhost when defining a RabbitMQ resource, the same way the username and password can be set when calling `RabbitMQBuilderExtensions.AddRabbitMQ`:
```csharp
// before: health check will fail
builder.AddRabbitMQ("rabbitmq")
.WithEnvironment("RABBITMQ_DEFAULT_VHOST", "custom-vhost");

// after
builder.AddRabbitMQ("rabbitmq", defaultVHost: "custom-vhost");
```

I tried to come up with a pull request but had trouble building it in Linux (`Interop+Crypto+OpenSslCryptographicException`). I'll try again when I'm using Windows.

I think the `RabbitMQServerResource` could be changed to expose a `DefaultVirtualHost` property, so that it can be used in the connection string and consequently be picked up by the health check added in `RabbitMQBuilderExtensions.AddRabbitMQ`

```csharp
public class RabbitMQServerResource : ContainerResource, IResourceWithConnectionString, IResourceWithEnvironment
{
// . . . ommitted

///
/// Gets the parameter that contains the RabbitMQ default virtual host.
///
public ParameterResource? DefaultVirtualHost { get; }

internal ReferenceExpression DefaultVirtualHostReference =>
DefaultVirtualHost is not null ?
ReferenceExpression.Create($"/{DefaultVirtualHost}") :
ReferenceExpression.Create($"");

///
/// Gets the connection string expression for the RabbitMQ server.
///
public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create(
$"amqp://{UserNameReference}:{PasswordParameter}@{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}{DefaultVirtualHostReference}");
}
```

### Additional context

[Repro sample](https://github.com/danspark/aspire-rabbitmq-default-vhost-repro/blob/main/Aspire.RabbitMqDefaultVhost.AppHost/Program.cs): health check will fail because it tries to connect to vhost `/`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.