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.