Mediatr Polymorphic dispatch of request handler not working

653 views Asked by At

I have a Mediatr request handler that I want to take one of two different (related) requests. When I use the basic request it works fine, but when I use the PermissionedGetInformationRequest, I get an error indicating it can't find the handler for my request.

Error constructing handler for request of type MediatR.IRequestHandler2[PermissionedGetInformationRequest, DataIWantView]. Register your handlers with the container

I'm not sure if I just haven't configured my IoC container properly, or the container doesn't support what I'm trying to do, or if I'm trying to do something that isn't supported by Mediatr (but I'm pretty sure it is, I seem to remember we had some similar code that worked ok using our old IoC, StructureMap)

I have a request:

public class GetInformationRequest : IRequest<DataIWantView>
{
}

and a request which is inherited from that

public class PermissionedGetInformationRequest : GetInformationRequest  
{
    public int RequesterId { get;set; }
}

and a request handler:

public class GetInformationHandler : IRequestHandler<GetInformationRequest, DataIWantView>
{
    public Task<DataIWantView> Handle(GetInformationRequestrequest, CancellationToken cancellationToken)
    {
        if (request is PermissionedGetInformationRequest permissionedRequest)
        {
            // Check permission
        }

        // Get the data I want
    }
}

I use dryioc as my IoC container, and to register my handlers I use

container.RegisterMany(new[] { typeof(GetInformationHandler).Assembly }, Registrator.Interfaces, made: PropertiesAndFields.Auto);
1

There are 1 answers

0
Anduril On

Issue was resolved by using Rules.WithVariantGenericTypesInResolve(). Despite my original comment above, I just wasn't setting the rules on the container correctly. Once I set the rule, it worked perfectly.