在 MassTransit 中以 SingleActiveConsumer 模式向 queue 发送消息

Sending message to queue in SingleActiveConsumer mode in MassTransit

我已经在 SingleActiveConsumer 模式下注册了接收端点。但是,我找不到使用 sendEndpoint 将消息直接发送到 queue 的方法。我收到以下错误:

The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text='PRECONDITION_FAILED - inequivalent arg 'x-single-active-consumer' for queue 'test' in vhost '/': received none but current is the value 'true' of type 'bool'', 

我尝试使用总线配置器设置 header "x-single-active-consumer"=true:

    var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
        {
            cfg.Host("localhost", "/", h =>
            {
                h.Username("guest");
                h.Password("guest");
            });
            cfg.ConfigureSend(a => a.UseSendExecute(c => c.Headers.Set("x-single-active-consumer", true)));
        });

并直接在 sendEndpoint 上:

        await sendEndpoint.Send(msg, context => {
                context.Headers.Set("x-single-active-consumer", true);
                });

如果你想直接发送到 MassTransit 中的接收端点,你可以使用短地址 exchange:test 代替,这将发送到交换而不尝试 create/bind 队列到交换同名。这样,您就可以将队列配置与消息生成器分离。

或者,您可以只使用 Publish,让交换绑定将消息路由到接收端点队列。