Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ CreateConneciton issues - works in one app but not in another

Tags:

c#

wpf

rabbitmq

So here is the connection code.

var factory = new ConnectionFactory
{
    HostName = "myserver",
    UserName = "testuser",
    Password = "testuserpassword"
};

using (var connection = factory.CreateConnection())
 using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "LOG",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: "LOG",
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

I test this in a console app and everything works fine and I could send and receive messages.

If i then copy and paste the same code from above into my WPF app, I get an exception here

connection = factory.CreateConnection()

Exception

Exception thrown: 'System.ArgumentException' in RabbitMQ.Client.dll

Additional information: No ip address could be resolved for myserver

If I change "myserver" to the server ip I get the same error. Don't understand why the code works in one app and not the other.

like image 559
Gaz83 Avatar asked Jan 03 '17 10:01

Gaz83


People also ask

How many connections can RabbitMQ handle?

The system is designed to cope with 20,000 connected clients, but we have been asked if it is possible to scale the system to handle up to 2,000,000 concurrent connections. 100 times the load the system originally were designed for.

Can not connect to RabbitMQ?

Make sure the node is running using rabbitmq-diagnostics status. Verify config file is correctly placed and has correct syntax/structure. Inspect listeners using rabbitmq-diagnostics listeners or the listeners section in rabbitmq-diagnostics status. Inspect effective configuration using rabbitmq-diagnostics environment.

Why is RabbitMQ connection closed?

A common cause for connections being abruptly closed as soon as they're started is a TCP load balancer's heartbeat. If this is the case you should see these messages at very regular intervals, and the generally accepted practice seems to be to ignore them.

What is churn in RabbitMQ?

A system is said to have high connection churn when its rate of newly opened connections is consistently high and its rate of closed connection is consistently high. This usually means that an application uses short lived connections.


Video Answer


2 Answers

Problem solved. It looks like it was something as simple as the Exception settings. For some reason the console app was set to not break on the connection exceptions and the WPF app was set to break. Everything now works.

Strange that the exceptions are being generated, especially about not resolving the server name or IP address but yet it still works.

like image 162
Gaz83 Avatar answered Oct 22 '22 21:10

Gaz83


It did because the exception setting, if you look into the RMQ .net client source code, at the beginning it will try to connect your ip address with IPv6 Protocol, if your are connecting an IPv4 address this step will fail and throw System.ArgumentException: No ip address could be resolved for 'your ip address', but the RMQ .net client will catch this exception and move forward to try connect your ip address with IPv4 Protocol.

 if (ShouldTryIPv6(endpoint))
        {
            try {
                m_socket = ConnectUsingIPv6(endpoint, socketFactory, connectionTimeout);
            } catch (ConnectFailureException)
            {
                m_socket = null;
            }
        }

        if (m_socket == null && endpoint.AddressFamily != AddressFamily.InterNetworkV6)
        {
            m_socket = ConnectUsingIPv4(endpoint, socketFactory, connectionTimeout);
        }

if yous set to break on the connection exceptions, your force the ArgumentException to throw.

  public virtual async Task ConnectAsync(string host, int port)
    {
        AssertSocket();
        var adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
        var ep = TcpClientAdapterHelper.GetMatchingHost(adds, sock.AddressFamily);
        if (ep == default(IPAddress))
        {
            throw new ArgumentException("No ip address could be resolved for " + host);
        }
like image 33
Dannyg Avatar answered Oct 22 '22 19:10

Dannyg