I'm trying to use MassTransit mediator on my Asp.Net web API. Currently I'm using Masstransit 7.3.0
I wanted for consumers to share the same scope. So I did everything step-by-step as in documentation.
Here I'm setting up the mediator in startup and adding the consumers, requestclients:
services.AddHttpContextAccessor();
services.AddMediator(cfg =>
{
cfg.ConfigureMediator((regContext, options) =>
{
options.UseHttpContextScopeFilter(regContext);
});
cfg.AddConsumer<BillingFile.Create>();
cfg.AddRequestClient<BillingFile.Command.CreateCommand>();
cfg.AddConsumer<Payment.CreateBasePayment>();
cfg.AddRequestClient<Payment.Command.CreateBasePaymentCommand>();
});
I'm using HttpContextScopeFilter
directly from the documentation on the website.
When I try to send a message from one consumer to the other, an exception is thrown:
The ConsumeContext was already set
Here is my Create
consumer:
public class Create : IConsumer<CreateCommand>
{
private readonly IMediator mediator;
public Create(IMediator mediator)
{
this.mediator = mediator;
}
public async Task Consume(ConsumeContext<CreateCommand> context)
{
var request = context.Message.Request;
/* Create base payment <--- here be the exception */
var basePaymentHandler = mediator.CreateRequestClient<CreateBasePaymentCommand>(context);
var basePaymentResponse = await basePaymentHandler.GetResponse<CreateBasePaymentResponse>(new CreateBasePaymentCommand { Request = Request, CorrelationId = Guid.NewGuid()}, cancellationToken: context.CancellationToken);
....
Is this not allowed by Masstransit mediator?
MassTransit won't let you dispatch multiple messages in the same container scope. It results in the very exception you are experiencing. You're also using an obsolete/unsupported version of MassTransit.