无法配置 MassTransit 发送拓扑 correlationId

Unable to configure MassTransit send topology correlationId

我有两个 API,一个用于发送消息,另一个带有消费者传奇以使用该消息。在发送端,我的消息模型没有实现 CorrelatedBy<> 接口,但有一个我想用作 correlationId 的字段。据我从文档中了解到,应该配置如下。

GlobalTopology.Send.UseCorrelationId<SubmitOrder>(x => x.OrderId);

但是,在设置之后,我在我的消息中没有看到 correlationId 一旦被我的消费者传奇消费,它是一个空的 GUID。

我还尝试了文档中概述的另一种方法,但在我的传奇中也没有产生 correlationId。

Bus.Factory.CreateUsingRabbitMQ(..., cfg =>
{
    cfg.Send<OrderSubmitted>(x =>
    {
        x.UseCorrelationId(context => context.Message.OrderId);
    });
});

消息接口

public interface MyEvent {
    Guid MyId { get; }
    DateTime Timestamp { get; }
}

注册

builder.AddMassTransit(x => {
    x.Builder.RegisterBuildCallback(bc => {
        var bus = bc.Resolve<IBusControl>();
        bus.Start();
    });

    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.UsingRabbitMq((context, cfg) => {
        cfg.Send<MyEvent>(x => {
            x.UseCorrelationId(ctx => ctx.MyId);
        });

        cfg.Host(new Uri(Configuration["RabbitMqHost"]), host => {
            host.Username(Configuration["RabbitMqUsername"]);
            host.Password(Configuration["RabbitMqPassword"]);
        });

        cfg.ReceiveEndpoint(Configuration["RabbitMqQueueName"], ec => {
            ec.ConfigureConsumers(context);
        });

        cfg.ConfigureEndpoints(context);
    });

});

我哪里错了?我希望 MyId 的值在发送时分配给 CorrelationId,并在另一端显示在我的传奇的消费上下文中。

这里的解决方法很简单,遵循约定并将我的字段命名为 CorrelationId,一切都按预期工作。

Consumer sagas 使用 CorrelatedBy<Guid> 接口进行关联,该接口必须在消息协定上。他们不在 ConsumeContext.

上使用 CorrelationId 属性