2
votes

Using Mediatr, I have the following request handler:

public class GetEntityByIdRequest<TEntity> : IRequest<TEntity> where TEntity : Entity
{
    public int Id { get; set; }

    internal class Handler : IRequestHandler<GetEntityByIdRequest<TEntity>, TEntity>
    {
        public TEntity Handle(GetEntityByIdRequest<TEntity> message)
        {
            return new Session.Query<TEntity>().FirstOrDefault(x => x.Id == message.Id);
        }
    }
}

I am having trouble registering this generic request handler in my IoC. I have tried registering like:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>));
container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<Entity>));

Which gives me the error:

System.ArgumentException : The supplied type GetEntityByIdRequest<TEntity> does not implement IRequestHandler<TRequest, TResponse>. Parameter name: serviceType

I have also looked at this which is identical to the issue I am having, but the person is using StructureMap as opposed to Simple Injector.

Can someone help me register my generic request handler.

1

1 Answers

2
votes

You are registering the query type as a handler. This will obviously not work. You will have to register the handler instead:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>.Handler));