Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to RabbitMQ started with Docker in a C# program (with RabbitMQ.Client)

I started a RabbitMQ container following the article: https://docs.docker.com/samples/library/rabbitmq using the image including the management tool

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management
docker container ls --all
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS
         NAMES
8a42bb749074        rabbitmq:3-management   "docker-entrypoint.s…"   8 hours ago         Up 8 hours          4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:8080->15672/tcp   some-rabbit

I can access the management tool via http://localhost:8080

I then created the most basic C# Project ever following that article to communicate with my local instance of RabbitMQ:

using System.Globalization;
using RabbitMQ.Client;

namespace RabbitCSharp
{
    public static class Program
    {
        public static void Main(params string[] args)
        {
            var factory = new ConnectionFactory
            {
                HostName = "localhost"
            };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                }
            }
        }
    }
}

But unfortunately ran in the following error:

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException
: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.S
ocketExceptionFactory+ExtendedSocketException: No connection could be made because the target machine actively refused it 127.0.0.1:5672
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__272_0(IAsyncResult iar)
--- End of stack trace from previous location where exception was thrown ---
   at RabbitMQ.Client.TcpClientAdapter.ConnectAsync(String host, Int32 port)
   at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, Int32 millisecondsTimeout)
   at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, Int32 timeout)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 timeout, AddressFamily fami
ly)
   at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 wri
teTimeout)
   at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(AmqpTcpEndpoint endpoint)
   at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
   at RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(IEndpointResolver endpoints)
   at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
   at RabbitCSharp.Program.Main(String[] args) in C:\Users\eperret\Desktop\RabbitCSharp\RabbitCSharp\RabbitCSharp\Program.cs:line 14

Process finished with exit code -532,462,766.

Not really sure to understand why the RabbitClient cannot make it through 127.0.0.1:5672

Any idea?

like image 253
Natalie Perret Avatar asked Jan 26 '23 09:01

Natalie Perret


1 Answers

It seems that my container port 5672 was not mapped to the local port 5672.

I just created another container including the port mapping like shown in this answer:

docker stop some-rabbit
docker commit some-rabbit some-rabbit-right
docker run -p 5672:5672 -p 8080:15672 -td some-rabbit-right

Or to run directly a new instance:

docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 8080:15672 rabbitmq:3-management
like image 128
Natalie Perret Avatar answered Jan 29 '23 07:01

Natalie Perret