3

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

asked Jul 5, 2025 at 12:02
2
  • 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. Commented 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. Commented 8 hours ago

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.