单个消费者绑定到多个端点

问题描述 投票:0回答:1

我正在尝试将单个消费者绑定到多个端点。这可能吗?

我的消费者

class OrderPlacedConsumer : IConsumer<OrderPlaced>
{
    public Task Consume(ConsumeContext<OrderPlaced> context)
    {
        //do stuff
    }
}

我注册消费者和巴士的方式。

services.AddMassTransit(x =>
{
    x.AddConsumer<OrderPlacedConsumer>();

    x.UsingRabbitMq((ctx, cfg) =>
    {
        //Read RabbitMq setting from config 
        string connectionString = config.ConnectionStrings.RabbitMQConnString;
        RabbitMQSetting rabbitMQSetting = GetRabbitMQSetting(connectionString);
        cfg.Host(rabbitMQSetting.Host, (ushort)rabbitMQSetting.Port, "/", h =>
        {
            h.PublisherConfirmation = false;
            h.Username(rabbitMQSetting.UserName);
            h.Password(rabbitMQSetting.Password);
            h.Heartbeat(TimeSpan.FromSeconds(rabbitMQSetting.Heartbeat));
        });

        cfg.ReceiveEndpoint("endpoint1", e =>
        {
            e.ConfigureConsumer(ctx, OrderPlacedConsumer);

            #region Exchange
            e.ConfigureConsumeTopology = false;
            e.Bind("OrderExchange", x =>
            {
                x.ExchangeType = ExchangeType.Direct;
                x.RoutingKey = "endpoint1";
            });
            #endregion
        });

        cfg.ReceiveEndpoint("endpoint2", e =>
        {
            e.ConfigureConsumer(ctx, OrderPlacedConsumer);

            #region Exchange
            e.ConfigureConsumeTopology = false;
            e.Bind("OrderExchange", x =>
            {
                x.ExchangeType = ExchangeType.Direct;
                x.RoutingKey = "endpoint2";
            });
            #endregion
        });
    });
});

但是,“OrderPlacedConsumer”消费者仅绑定到“endpoint1”。是否也可以将其绑定到“endpoint2”。

c# masstransit
1个回答
0
投票

它应该使用正确的语法正确配置消费者:

e.ConfigureConsumer<OrderPlacedConsumer>(ctx);
© www.soinside.com 2019 - 2024. All rights reserved.