Repository: MassTransit/MassTransit

Issue #946: Remote RabbitMQ Connection Timeout using .NET Core 2.0
Created: 2017-07-25T05:12:20Z
I am not sure if this is due to Core 2.0 but I can run it fine on non core.
The code I am using is below.

Options

  "RabbitMQ": {
    "Host": "rabbitmq://192.168.160.131/",
    "Username": "test",
    "Password": "1"
  },

Bus

            var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host(new Uri(_rabbitMQOptions.Host), h =>
                {
                    h.Username(_rabbitMQOptions.Username);
                    h.Password(_rabbitMQOptions.Password);
                });


                cfg.ReceiveEndpoint(host, nameof(TestCommand), e =>
                {
                    e.Consumer(() =>
                    {
                        return new TestConsumer();
                    });

                    e.PrefetchCount = 2;
                });

            });


            _logger.LogInformation("Starting bus");
            busControl.Start();
            _logger.LogInformation("Bus started");
            _logger.LogInformation("Waiting for messages");

Command & Consumer

    public class TestCommand
    {
    }

    public class TestConsumer : IConsumer<TestCommand>
    {

        public TestConsumer()
        {
        }

        public async Task Consume(ConsumeContext<TestCommand> context)
        {
            Console.WriteLine("Consumed");
        }
    }
When I run the code, it fails with
'Connect failed: test@192.168.160.131:5672/'
TimeoutException: The operation has timed out.
This issue occurs with both 3.5.7 and 4.0.0.1216-develop.
I have confirmed that I can connect fine by following the rabbitmq hello world tutorial.
Each connection creates the following log entries in rabbitmq.
=INFO REPORT==== 25-Jul-2017::05:00:19 ===
accepting AMQP connection <0.31858.0> (192.168.160.1:51098 -> 192.168.160.131:5672)

=ERROR REPORT==== 25-Jul-2017::05:00:19 ===
closing AMQP connection <0.31858.0> (192.168.160.1:51098 -> 192.168.160.131:5672):
{handshake_timeout,handshake}

Duplicate issue #985: RabbitMQ connection failed on .Net Core
Created: 2017-09-01T12:52:32Z
When trying to connect to RabbitMQ with an IP address with .Net Core the following exception happens. If you wrap the bus.Start() in a retry block using a library like Polly after 10-40 attempts it will eventually connect and start. Connecting to my localhost RabbitMQ instance works correctly.
Unhandled Exception: MassTransit.RabbitMqTransport.RabbitMqConnectionException: Connect failed: user@00.00.000.000:5672/ ---> RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.TimeoutException: The operation has timed out.
   at RabbitMQ.Client.Impl.TaskExtensions.<TimeoutAfter>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, Int32 timeout)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
   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 MassTransit.RabbitMqTransport.Integration.RabbitMqConnectionCache.SendUsingNewConnection(IPipe`1 connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Integration.RabbitMqConnectionCache.ConnectionScope.<Attach>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Integration.RabbitMqConnectionCache.<SendUsingExistingConnection>d__12.MoveNext()
   --- End of inner exception stack trace ---
   at MassTransit.RabbitMqTransport.Integration.RabbitMqConnectionCache.<SendUsingExistingConnection>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Transport.RabbitMqReceiveTransport.<>c__DisplayClass15_0.<<Receiver>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.Transports.ReceiveEndpointCollection.<StartEndpoint>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Transport.RabbitMqHost.<Start>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.MassTransitBus.<StartAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MassTransit.RabbitMqTransport.Transport.RabbitMqReceiveTransport.<>c__DisplayClass15_0.<<Receiver>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.Policies.PipeRetryExtensions.<Retry>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MassTransit.MassTransitBus.<StartAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.Util.TaskUtil.Await[T](Func`1 taskFactory, CancellationToken cancellationToken)
   at MassTransit.BusControlExtensions.Start(IBusControl busControl)
   at BusTest.Program.Main(String[] args) 

Very basic example:
using System;
using MassTransit;

namespace BusTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://00.00.000.000:5672"), h =>
                {
                    h.Username("user");
                    h.Password("password");
                });
            });

            bus.Start();
        }
    }
}

csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MassTransit" Version="4.0.0.1274-develop" />
    <PackageReference Include="MassTransit.RabbitMQ" Version="4.0.0.1274-develop" />
  </ItemGroup>

</Project>

Closing comment (2017-09-01T13:33:24Z): Duplicate of #946