[RabbitMQ Hosting] Allow setting a default vhost
- 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
Assessment
This issue has not been assessed yet.