I'm switching from MediatR to Wolverine. It's handy to use. However, I have run into one problem. I want to add middleware to check authorization before using the handler. Which is not a problem either. But the problem is to return some result directly from the middleware. Which is normally possible in MediatR (behaviour). So the question is - is there any elegant way to return a result directly from the middleware?
I've read the wolverine documentation tried all possible variations and nothing helped. If I'm not mistaken, it is possible to return the middleware result as a handler parameter, but that seems extremely unsightly to me. I would then have to have each handler check if it contains something etc and other problems.
I don't want to use the Wolverine.HTTP library. I will use this logic in other places than WebApi.
OutputDto
public class OutputDto
{
public bool Valid { get; set; }
}
MyHandler
public class MyHandler
{
public Task<OutputDto> Handle(InputDto input)
{
return Task.FromResult(new OutputDto() { Valid = true });
}
}
MyMiddleware
public class MyMiddleware
{
public async Task<(HandlerContinuation, OutputDto)> BeforeAsync(InputDto input)
{
return (HandlerContinuation.Stop, new OutputDto() { Valid = false });
}
}
Controller - action invoke middleware and return null
private readonly IMessageBus _bus;
public WeatherForecastController(IMessageBus bus)
{
_bus = bus;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<OutputDto> Get()
{
var a = await _bus.InvokeAsync<OutputDto>(new InputDto { Id = 5 });
return a;
}
Registration
builder.UseWolverine(options =>
{
options.Policies.AddMiddleware<MyMiddleware>();
});
Thank you
-
I think I have the same problem, i want to return something from the middleware, aware that the return type must match the invoke type, but the response from inkove is always null. works well when returning something from handler, but even the simplest example like returning a string from the middleware (invoke<string>) doest not work. there is even an example in the docs wolverinefx.net/tutorials/middleware.html I dont know what I/we are doing wrong... still investigating.Kenny Fowler– Kenny Fowler2025年09月12日 01:01:44 +00:00Commented Sep 12, 2025 at 1:01
-
I faced the same issue as well. But found a solution by trying in various ways. Still trying to implement a well-structured solution. I will share my solution with you after that.Abir Mahmud– Abir Mahmud2026年01月05日 20:37:16 +00:00Commented 8 hours ago