简体   繁体   中英

Does publishing a message on the same ConsumeContext that received it have any benefits?

I'm asking about a situation where the consumer gets the message, processes it and then the result of that processing is another message, so something like:

class MyConsumer : IConsumer<MyMessage> {
    public async Task Consume(ConsumeContext<MyMessage> context) {
        // ...do the processing and then:
        await context.Publish<MyResponse>(new()
        {
            Data = "some data"
        });
    }
}

So the question is - does using context.Publish have any benefits over injecting IPublishEndpoint ? It would be done if the processing would require another component to be separated from the consumer - another class. Then the result of that component processing would be a message which could be published by the injected IPublishEndpoint .

In a consumer (and any of the consumers dependencies), messages published and/or sent should use:

  • ConsumeContext - easiest in the consumer, since it already has it as part of the method signature.
  • ISendEndpointProvider or IPublishEndpoint - should be injected into any dependencies of the consumer that need to produce messages. MassTransit is essentially redirecting these two interfaces to the current ConsumeContext behind the scenes.

This is also covered in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM