1
votes

I'm new to docker. I created a .net core 5.0 web api project.

UPDATE Originally, I was getting the error below on startup. I'm currently receiving the error when attempting to send my message.

Goal: Send message via masstransit

Error: warn: MassTransit[0] Retrying 00:00:08.1220000: Broker unreachable: guest@localhost:5672/ MassTransit.RabbitMqTransport.RabbitMqConnectionException: Broker unreachable: guest@localhost:5672/ ---> 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.Sockets.SocketException (111): Connection refused

I created a docker-compose file

version: '3.4'

networks:
  poc:

services:
  rabbitmq:
    image: masstransit/rabbitmq:latest
    networks:
    - poc
    container_name: rabbitmq
    environment:
        RABBITMQ_ERLANG_COOKIE: ${RABBITMQ_ERLANG_COOKIE}
        RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
        RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
    ports:
        - 5672:5672
        - 15672:15672
  webapi:
    image: ${DOCKER_REGISTRY-}webapi
    build:
      context: .
      dockerfile: WebApi/Dockerfile
    ports:
        - 5100:80
    
  test:
    image: ${DOCKER_REGISTRY-}test
    build:
      context: .
      dockerfile: Test/Dockerfile
    ports:
        - 5101:80

In my startup.cs on the WebApi side...

services.AddMassTransit(mt =>
{
    mt.UsingRabbitMq((context, cfg) =>
    {
        cfg.AutoStart = true;
        cfg.Host("rabbitmq");
        cfg.UseRabbitMqMessageScheduler();
        cfg.ConfigureEndpoints(context);
    });
});

My controller

public class OrderController : Controller
    {
        private readonly IBusControl _bus;

        public OrderController(IBusControl bus)
        {
            _bus = bus;
        }

        [HttpPost("api/order")]
        public async Task<IActionResult> Index([FromBody] Order order)
        {
            try
            {
                Uri uri = new Uri("queue:order-queue");
                var endpoint = await _bus.GetSendEndpoint(uri);
                await endpoint.Send(order);
                return Ok("Success");
            }
            catch (Exception exception)
            {
                return StatusCode(500, exception);
            }
        }
    }
1

1 Answers

2
votes

When I run services in Docker, I find that localhost doesn't work. So I use a check to see if I'm in a container and use the rabbitmq hostname (or whatever the host name of your RabbitMQ container is in docker-compose.yml.

static bool? _isRunningInContainer;

static bool IsRunningInContainer =>
    _isRunningInContainer ??= bool.TryParse(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), out var inContainer) && inContainer;

Then, in UsingRabbitMq:

if (IsRunningInContainer)
    cfg.Host("rabbitmq");

A working example if you need to see how it's all wired up.