Search code examples
c#rabbitmqhealth-check

How to find RabbitMQ connection string


I am trying to add HealthCheck for RabbitMQ.

First off.

  1. Its running
  2. it works I can queue things to it.

This is not an issue with RabbitMQ this is an issue with the HealthCheck connecting to RabbitMQ

I have added this to my Configure services.

services
    .AddHealthChecks()
    .AddRabbitMQ(rabbitConnectionString:"amqps://guest:guest@localhost/vhost");

But it throws an exception.

fail: Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService[103] Health check rabbitmq completed after 4144.5115ms with status Unhealthy and description '(null)' RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable

Obously there is something wrong with how I am building the connection string but i have tried everything I can think of.

Its not "amqps://guest:guest@localhost/rabbit" either

This is a dev version so the defaults are all in place.

I was wondering if it had something to do with the name in the UI

enter image description here

but my code that actually reads and writes from the queue use localhost so I'm not sure why localhost wouldn't work.


Solution

  • quite late to reply but I had the same issue just now.

    the way I fixed the issue is using the overload with IConnection instead of the string connectionstring one.

    private static IConnection GetRabbitMqConnection(IServiceProvider _)
    {
        ConnectionFactory factory = new ConnectionFactory();
        factory.UserName = "user";
        factory.Password = "password";
        factory.VirtualHost = "/vhost";
        factory.HostName = "localhost";
        factory.Port = AmqpTcpEndpoint.UseDefaultPort;
        IConnection conn = factory.CreateConnection();
    }
    

    and then

    services
        .AddHealthChecks()
        .AddRabbitMQ(GetRabbitMqConnection);
    

    That is giving me healthy responses.