Upgrading from Mediator 3.0.2 to 3.1.0-rc.1 seems to introduce a bug with ASP.NET Core Minimal APIs when using CachingMode.Lazy:.
A route like this used to work:
app.MapPost(
"/test",
async (IMediator mediator, MyCommand command) => await mediator.Send(command)
);
where MyCommand is a normal mediator message:
public sealed record MyCommand(string Value) : ICommand<string>;
public sealed class MyCommandHandler : ICommandHandler<MyCommand, string>
{
public ValueTask<string> Handle(MyCommand command, CancellationToken cancellationToken)
=> ValueTask.FromResult(command.Value);
}
After upgrading, Minimal APIs appear to treat MyCommand as a DI service instead of binding it from the request body.
That leads to runtime failures like:
System.InvalidCastException: Unable to cast object of type '...CommandHandlerWrapper<...>' to type 'MyCommand'
It looks like AddMediator(...) in 3.1.0-rc.1 now registers message types themselves as services, which changes ASP.NET Core parameter binding behavior.
Workaround is to make body binding explicit:
app.MapPost(
"/test",
async (IMediator mediator, [FromBody] MyCommand command) => await mediator.Send(command)
);
Upgrading from
Mediator 3.0.2to3.1.0-rc.1seems to introduce a bug with ASP.NET Core Minimal APIs when using CachingMode.Lazy:.A route like this used to work:
where MyCommand is a normal mediator message:
After upgrading, Minimal APIs appear to treat MyCommand as a DI service instead of binding it from the request body.
That leads to runtime failures like:
System.InvalidCastException: Unable to cast object of type '...CommandHandlerWrapper<...>' to type 'MyCommand'It looks like AddMediator(...) in 3.1.0-rc.1 now registers message types themselves as services, which changes ASP.NET Core parameter binding behavior.
Workaround is to make body binding explicit: