Unable to send message via masstransit Broker unreachable: [email protected]:5672/

  .net-core, c#, docker, masstransit, rabbitmq

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

Goal: Send message via masstransit

Problem: Receiving error message at startup
MassTransit.RabbitMqTransport.RabbitMqConnectionException: Broker unreachable: [email protected]:5672/
—> RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
—> System.AggregateException: One or more errors occurred. (Connection failed)

My docker-compose.yml file looks like this

version: '3.8'

services:
    rabbitmq3:
        image: rabbitmq:3-management
        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

My Dockerfile looks like this

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["WebApi/WebApi.csproj", "WebApi/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY –from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]

Inside my Startup.cs my DI point looks like


services.AddMassTransit(options =>
            {
                options.AddConsumer<OrderConsumer>();
                
                options.UsingRabbitMq((context, cfg) =>
                {
                    MessageDataDefaults.ExtraTimeToLive = TimeSpan.FromDays(1);
                    MessageDataDefaults.Threshold = 2000;
                    MessageDataDefaults.AlwaysWriteToRepository = false;
                });

                options.AddRequestClient<Order>(new Uri($"queue:{KebabCaseEndpointNameFormatter.Instance.Consumer<OrderConsumer>()}"));
            });

            services.AddMassTransitHostedService();

When I run docker-compose up I’m able to hit localhost:15672. However, when I run the app I get the above error, and when I hit my api, it just hangs.

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

Source: Docker Questions

LEAVE A COMMENT