Skip to content

3.1.0-rc.1 CachingMode.Lazy: Minimal API body parameters are inferred as DI services after AddMediator #278

Description

@irunjic

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)
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions