RabbitMQ Connection refused 127.0.0.1:5672

Viewed 5436

I am preparing a simple ASP.NET Core MVC web application.

I have installed RabbitMQ server to my laptop. RabbitMQ Management UI is running on localhost:15672.

Rabbit MQ cluster name is like: rabbit@CR00001.ABC.COM.LOCAL

I am trying to send message to rabbitmq in controller. But I am getting None of the specified endpoints were reachable error.

If I use 'localhost' as host name, I get Connection refused 127.0.0.1:5672 in inner exceptions.

If I use rabbit as host name, I get Name or service not known

I've tried to solve the problem according to other StackOverflow questions, however, none of them could solved my problem.

Home controller:

        [HttpPost]
        public void SendMessage([FromBody]Message message)
        {
            try
            {
                var factory = new ConnectionFactory()
                {
                    UserName = _username,
                    Password = _password,
                    HostName = _hostname,
                    VirtualHost = "/",
                    Port = _port,
                };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: _queueName,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var body = Encoding.UTF8.GetBytes(message.Text);

                    channel.BasicPublish(exchange: "",
                                         routingKey: _queueName,
                                         basicProperties: null,
                                         body: body);
                }

            }
            catch (Exception ex)
            {

            }
        }

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "RabbitMq": {
    "Hostname": "localhost",
    "QueueName": "WordQueue",
    "UserName": "test",
    "Password": "test",
    "Port": 5672
  }
}

Here is test user configuration in Rabbit MQ Management UI test user configuration

2 Answers

Have you setup your test user in the UI portal? This is probably the cause of your connection refused error. You can setup users via http://localhost:15672/#/users.

You should also debug your factory to check that your config values are being passed in correctly

I would also suggest that you pop some code in your catch to ensure you aren't missing an exception

The references to http://rabbit are for use within containers. These will only work if you are running both your ASPNET and Rabbit applications within a containerised network (for example Docker Compose). I found using containers was a much better approach for learning RabbitMq.

Apprciate that this is going a little off-topic now, but if you are not familiar with containerisation I would suggest taking a look at this post (and the respective series) from Wolfgang Ofner https://www.programmingwithwolfgang.com/rabbitmq-in-an-asp-net-core-3-1-microservice/ and the getting started with Docker from Brad Traversy on YouTube https://www.youtube.com/watch?v=Kyx2PsuwomE

After create user, do not forget set permissions, basicaly,

  • can access virtual hosts (/)

  • set topic permission (AMQP default) Note: of course you can use rabbitmq ui for this operation (create user and permissions). enter image description here

    var factory = new ConnectionFactory() { HostName = "hostname_or_ip_adres_here", UserName="username here..", Password="psw here.." };

this will work !

Related