Masstransit 如何进行高级请求/回复模式

Masstransit How to make Advanced request / reply pattern

我有 3 个微服务,如下通过 masstransit/rabbitmq

进行通信

我希望 ApiTransactionService 发出请求,但得到了 PspService 的响应。

namespace API
{
    //I wish to have something like this
    PaymentForm form = await requestClient.GetResponse<PaymentForm>(new CreatePayment())
}

namespace TransactionService
{
    public class CreatePaymentConsumer : IConsumer<CreatePayment>
    {
        public async Task Consume(ConsumeContext<CreatePayment> context)
        {
            context.Send<BuildPaymentForm>()
        }
    }
}

namespace PspService
{
    public class BuildPaymentFormConsumer : IConsumer<BuildPaymentForm>
    {
        public async Task Consume(ConsumeContext<BuildPaymentForm> context)
        {
            context.Response<PaymentForm>() //how to make sure that the response will be sent to the API, but not to the caller (TransactionService)?
        }
    }
}

请指出制作此通信模式或类似示例的正确方法,或文档中的正确部分。

您可以将 RequestIdResponseAddressCreatePayment 消息复制到第一个消费者使用 CreateCopyContextPipe 方法生成的消息。还有一种 built-in 方法可以将所有 headers 复制到外发消息(我在下面使用过)。

namespace TransactionService
{
    public class CreatePaymentConsumer : IConsumer<CreatePayment>
    {
        public async Task Consume(ConsumeContext<CreatePayment> context)
        {
            var pipe = context.CreateCopyContextPipe();

            await _otherHost.Publish(new BuildPaymentForm(...), pipe);
        }
    }
}

namespace PspService
{
    public class BuildPaymentFormConsumer : IConsumer<BuildPaymentForm>
    {
        public async Task Consume(ConsumeContext<BuildPaymentForm> context)
        {
            await context.RespondAsync(new PaymentForm(...));
        }
    }
}

第一个消费者将命令发布给第二个消费者,将所有 headers 复制到出站消息中。然后第二个消费者会响应请求发起者。