【问题标题】:How to share scope context when using multibus with masstransit将 multibus 与 masstransit 一起使用时如何共享范围上下文
【发布时间】:2022-11-03 10:43:59
【问题描述】:

我正在尝试创建过滤器来填写消费者中的correlationId,并将此correlationId 添加到发送消息中。

但是,我相信,因为它是多总线,所以 2 个配置的总线之间的上下文是不同的,有什么办法可以使这项工作。

以下是其中一个过滤器的示例:

public class CorrelationSendFilter<T> :
    IFilter<SendContext<T>>
    where T : class
{
    private CorrelationContext _correlationContext;

    public CorrelationSendFilter(CorrelationContext correlationContext) { _correlationContext = correlationContext; }

    public async Task Send(SendContext<T> context, IPipe<SendContext<T>> next)
    {
        context.CorrelationId = _correlationContext.GetOrNewCorrelationId();

        await next.Send(context);
    }

    public void Probe(ProbeContext context) { }
}

CorrelationContext 是在 DI 中定义的

【问题讨论】:

  • 从一条总线上使用的消息复制上下文的能力正在发送到另一条总线上,这是没有 ETA 的路线图。

标签: .net-core masstransit


【解决方案1】:

如果您在默认总线上有一个使用者,并且您希望能够使用来自另一个总线的作用域接口(例如ISecondBus),您可以在构造函数上使用绑定语法,如下所示:

using MassTransit;
using MassTransit.DependencyInjection; // needed for Bind

class FirstMessageConsumer :
    IConsumer<FirstMessage>
{
    readonly IPublishEndpoint _publishEndpoint;

    public FirstMessageConsumer(Bind<ISecondBus, IPublishEndpoint> publishEndpoint)
    {
        _publishEndpoint = publishEndpoint.Value;
    }

    public async Task Consume(ConsumeContext<FirstMessage> context)
    {
        await _publishEndpoint.Publish(new PingMessage());
    }
}

这应该将 ConversationId 从消费消息复制到已发布消息,以及任何标头。由于 MassTransit 的标识符方法,消费消息的CorrelationId 将被复制到已发布消息的InitiatorId。如果需要Send,则同样适用于ISendEndpointProvider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2014-09-14
    • 1970-01-01
    相关资源
    最近更新 更多