I have gateway project that send data by in MassTransit, and wait to get the response.
I the consumer I add filter to exceptions – the problem is that the exception Reaches the GW project before reaching the filter.
Here the GW code:
private readonly IRequestClient<TokenContracts.FullRequest<REQ>> _Client;
public GWController(IRequestClient<TokenContracts.FullRequest<REQ>> submitTokenRequestClient)
{
_Client = submitTokenRequestClient;
}
[HttpPost("REQ")]
public async Task<IActionResult> Validate(TokenContracts.FullRequest<REQ> model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
var response = await _Client.GetResponse<TokenContracts.FullResponse<Res>>(model);
if (response?.Message?.Msg?.RcType != 1)
return BadRequest(response.Message);
return Accepted(response.Message);
}
catch (System.Exception ex)
{
return Problem(ex.Message);
}
}
here the consumer the startup.cs file:
services.AddMassTransit(config =>
{
config.AddConsumersFromNamespaceContaining<ConsumersLst>();
config.AddBus(context => Bus.Factory.CreateUsingRabbitMq(c =>
{
c.UseFilter(new MyExceptionFilter<BadRequestException>(context.GetRequiredService<ILogger<MyExceptionFilter<BadRequestException>>>()));
UseConsumeFilter(c, typeof(MyConsumeFilter<>), context);
c.Host("rabbitmq://10.236.38.64:5672/", h =>
{
h.Password("guest");
h.Username("guest");
});
c.ConfigureEndpoints(context, KebabCaseEndpointNameFormatter.Instance);
}));
});
here the consume:
public async Task Consume(ConsumeContext<FullRequest<REQ>> context)
{
var req = Encoding.UTF8.GetString((byte[])context.ReceiveContext.GetBody());
var res = await mediator.Send(context.Message.appData);
await context.RespondAsync<FullResponse<RES>>(res);
throw new BadRequestException("very bed thing happebd")
return;
}
and here the filter:
public class MyExceptionFilter<TException> : IFilter<ConsumeContext>
where TException : Exception
{
private readonly ILogger<MyExceptionFilter<TException>> _logger;
public MyExceptionFilter(ILogger<MyExceptionFilter<TException>> logger)
{
_logger = logger;
}
public async Task Send(ConsumeContext context, IPipe<ConsumeContext> next)
{
try
{
await next.Send(context);
}
catch (TException ex)
{
await HandleException(context, ex);
return;
}
}
my big problem is that when I debug my projects with breakPoints I see that after this line:
throw new BadRequestException("very bed thing happebd")
in the consumer ,I get to this line:
catch (System.Exception ex)
{
return Problem(ex.Message);
}
I the gw and just after it I get to this method in the filter:
catch (TException ex)
{
await HandleException(context, ex);
return;
}
because of that the exception comes to the filter without my changes in the filter
Someone can help?