You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MassTransit连接RabbitMQ时ACCESS_REFUSED权限问题咨询

Why MassTransit Accesses an Auto-Generated Exchange (And How to Fix the Permissions Issue)

Hey there! Great question—this is actually a default behavior of MassTransit, not a bug in your code. Let's break down what's going on and how to solve that tedious permission problem.

What Is That Auto-Generated Exchange?

The exchange named something like *ComputerName*_dotnet_bus_73451gfsgerqwrefxfddgf is MassTransit's bus endpoint exchange. It's critical for core bus functionality:

  • Handling internal operations like heartbeats and bus management commands
  • Routing response messages if you use request/response patterns later
  • Coordinating control messages between bus instances

When you start the bus, MassTransit first provisions this endpoint (exchange + matching queue) because the bus relies on it to function—this happens before it sets up your test receive endpoint, which is why you see the permission error first.

How to Fix the Permission Headache

Instead of manually adding permissions for every computer's auto-generated endpoint, you can customize the bus endpoint name to a fixed value. This way, you only need to set permissions once for that single name, no matter which machine runs the code.

Step 1: Customize the Bus Endpoint Name

Add a configuration block to set a static bus endpoint name when creating your RabbitMQ bus. Here's how to modify your code:

var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
{
    var host = sbc.Host(new Uri("rabbitmq://servername"), h =>
    {
        h.Username("user");
        h.Password("user");
    });

    // Add this line to set a fixed bus endpoint name
    sbc.Endpoint(x => x.Name = "my_fixed_bus_endpoint");

    sbc.ReceiveEndpoint(host, "test", ep =>
    {
        ep.Handler<Testing>(context =>
        {
            return Console.Out.WriteLineAsync($"Received: {context.Message.Id}");
        });
    });
});

Step 2: Update RabbitMQ Permissions

In the RabbitMQ management UI, grant your user permissions for:

  • The my_fixed_bus_endpoint exchange (configure, write, read access)
  • The corresponding my_fixed_bus_endpoint queue (write, read access)
  • Your existing test exchange and queue

That's it! Now MassTransit will use your fixed endpoint name instead of generating a computer-specific one, so you won't have to add new permissions for every machine.

Full Modified Code

Here's the complete code with the bus endpoint customization:

using System;
using MassTransit;

namespace Test
{
    public class Testing
    {
        public string Id { get; set; }
    }
}

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

                // Custom fixed bus endpoint name
                sbc.Endpoint(x => x.Name = "my_fixed_bus_endpoint");

                sbc.ReceiveEndpoint(host, "test", ep =>
                {
                    ep.Handler<Testing>(context =>
                    {
                        return Console.Out.WriteLineAsync($"Received: {context.Message.Id}");
                    });
                });
            });

            bus.Start();

            bus.Publish(new Testing { Id = "X" });

            Console.WriteLine("Waiting for messages. Press any key to exit");
            Console.ReadKey();

            bus.Stop();
        }
    }
}

Why This Works

By setting a fixed endpoint name, you override MassTransit's default behavior of generating a unique name based on the machine and process. This makes your bus endpoint consistent across all environments and machines, eliminating the need for repetitive permission setup.

内容的提问来源于stack exchange,提问作者inControl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:50:48