Skip to content

Using MapDiscoveredCommands with multiple aggregates results in error 'No handler found for command Object ' #264

@iappwebdev

Description

@iappwebdev

Describe the bug
In a ASP NET Web Application, app.MapDiscoveredCommands() is not working as expected. All correctly registered commands are found but all are failing on execution with error

{
  "type": "CommandHandlerNotFound`1",
  "title": "No handler found for command Object",
  "status": 500,
  "detail": "Eventuous.Exceptions+CommandHandlerNotFound`1[System.Object]: No handler found for command Object"
}

To Reproduce
See attached solution TestEventuous.zip with the following structure:
image

The solution contains Api, Application and Domain projects with commands for Car (StartEngine) and Train (OpenDoors) and the corresponding CommandServices.

[AggregateCommands<Car>]
public static class CarCommands
{
    [HttpCommand(Route = "car/start-engine")]
    public sealed record StartEngine
    {
        public required string CarId { get; init; }
    }
}
[AggregateCommands<Train>]
public static class TrainCommands
{
    [HttpCommand(Route = "train/open-doors")]
    public sealed record OpenDoors
    {
        public required string TrainId { get; init; }
    }
}
public sealed class CarService : CommandService<Car, CarState, CarId>
{
    public CarService(IAggregateStore? store, AggregateFactoryRegistry? factoryRegistry = null, StreamNameMap? streamNameMap = null, TypeMapper? typeMap = null)
        : base(store, factoryRegistry, streamNameMap, typeMap)
    {
        On<CarCommands.StartEngine>()
            .GetId(_ => new CarId(Guid.NewGuid().ToString()))
            .Act((car, _) => car.StartEngine());
    }
}
public sealed class TrainService : CommandService<Train, TrainState, TrainId>
{
    public TrainService(IAggregateStore? store, AggregateFactoryRegistry? factoryRegistry = null, StreamNameMap? streamNameMap = null, TypeMapper? typeMap = null)
        : base(store, factoryRegistry, streamNameMap, typeMap)
    {
        On<TrainCommands.OpenDoors>()
            .GetId(_ => new TrainId(Guid.NewGuid().ToString()))
            .Act((Train, _) => Train.OpenDoors());
    }
}

Running the application displays swagger with two endpoint car/start-engine and train/open-doors. Executing those endpoints yields the error described.

When changing
app.MapDiscoveredCommands(typeof(ApplicationModule).Assembly);
to
app.MapDiscoveredCommands<Car>(typeof(ApplicationModule).Assembly);
then car command is working as expected (but not train, but that is wanted, as described in the docs)

Expected behavior
All discovered commands should reach their corresponding handler.

Screenshots
image
image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions