MassTransit3如何向消费者提出要求

MassTransit3如何向消费者提出要求

本文介绍了MassTransit3如何向消费者提出要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从IConsumer.Consume()方法发出请求/请求,但是在ConsumeContext<>上没有任何方法返回对IRequestClient<,>的引用.我是否需要在某个地方保存对IBusControl的引用并使用它,或者可以以某种方式使用上下文?

I want to make a req/res request from IConsumer.Consume() method, but I don't see any method on ConsumeContext<> that returns a reference to IRequestClient<,>. Do I need to hold a reference to IBusControl somewhere and use it or I can use context somehow?

推荐答案

在这种情况下,最好在使用者外部创建请求客户端,并将其作为依赖项作为IRequestClient<,>接口传递给使用者.使用IBus创建请求客户端,该客户端不在使用者上下文中.

In this case, it is best to create the request client outside of the consumer and pass it as a dependency to the consumer as an IRequestClient<,> interface. The request client is created with IBus, which is outside of the consumer context.

这还可以确保请求不会与代理发生僵局,因为响应是在总线端点上接收的,而不是在使用者的接收端点上接收的(如果您的并发限制为1,则永远不会完成) ).

It also ensures that the request will be less likely to deadlock with the broker because responses are received on the bus endpoint, and not the receive endpoint of the consumer (which, if you had a concurrency limit of 1, would never finish).

也不可能将使用者连接到已启动的接收端点,这是请求/响应处理的要求(它在幕后进行).但是,总线可以连接使用者以发送消息(将响应发送到总线地址,而不是发布消息),这就是为什么将其用于响应请求的原因.

It's also not possible to connect consumers to a started receive endpoint, which is a requirement of the request/response handling (it happens under the covers). The bus, however, can connect consumers for messages which are sent (responses are sent to the bus address, versus being published) which is why it is used for responses to the requests.

要保持连续的消息跟踪,最好将出站请求上的InitiatorId设置为使用者消息的CorrelationId,并复制ConversationId.这有助于跟踪和跟踪整个消息命令/事件流.

To keep message tracking continuous, it may be nice to set the InitiatorId on the outbound request to the CorrelationId of the consumer message, as well as copying the ConversationId. This helps with tracing and keeping track of the overall message command/event flow.

这篇关于MassTransit3如何向消费者提出要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:06