-
Notifications
You must be signed in to change notification settings - Fork 89
Generic Request and Handler With AspNet Core DI #96
Comments
What you have is a partially closed generic type. None of the DI containers that I know of can't handle a partially closed type, where the generic parameter you want to fill in is somewhere inside the parameters passed into the overall generic type. What you'll need to do is explicitly register the closed generic types based on your known |
I think I am having a similar issue. I have this Handler with generics:
invoked here with generic:
from here:
resulting in this error:
I posted on StackOverflow too: https://stackoverflow.com/questions/67321156/mediatr-having-issue-resolving-handler-with-generics-using-microsoft-dependency |
That's a separate issue, and you'll need to post a more complete repro that includes your DI registration. |
Here's the mediatr registration
|
Any insight you can provide would be greatly appreciated. I would be in a great place if I got this one figured. |
What do you see as registered in the container? Are there any conflicts? |
Apologies, I don't understand what you mean. Could it be that the implemented types in the generic types are in different projects? |
No, so when there are "MediatR can't resolve XYZ" problems, I remove MediatR from the equation. Try to resolve that handler manually, from the container. Look at the container's |
After some more testing I need to register it with the DI container. I'm not quite sure how to do that with all the generics. |
Open generics tend to need to be registered explicitly. I register some cases, but not all. |
Ah, the readme is wrong, it says I register open |
How would you register the sample I posted? |
I tried:
|
For your case, you'll likely find that no container supports your case out of the box. The open generic type you have is The best you can do here is loop through your expected entities and DTOs and manually close the types and register the concrete types yourself. |
Alternatively, you can make that |
Yeah, I did that originally, I felt the concrete classes defeated the purpose. |
Like this? services.AddScoped(typeof(IRequestHandler<IRequest, Entity>), typeof(EntityHandlerGet<Dto,Entity>.Handler)); |
Yes, with the correct request/entity/dto types (not some base types). That's why creating concrete types might be a bit easier than looping through the "possible" types to close the generic types. I've done it before in simple cases (loop through all derived entity types and register say repositories). If it's too complex to loop through the possible types, concrete types even if they have no members or implementation is likely the simplest route. |
Got it working. Now that it's working on going to implement the loop through tactic you mentioned before.
Thank you for you help and Computer Science lesson. |
@craigmoliver Did you make the loop work? |
My approach to solve it with AspNet Core DI:
|
I have an issue with registering a request handler with generic request. I've searched everywhere, old issues, stack overflow and i couldn't find a single working answer. I've also tried everything suggested in any similar thread on this topic and still no luck.
I am using AspNet Core DI and MediatR version 9.0. Here is a sample of my request :
And the handler
Every time i have the same issue where it says handler couldn't be found for the request. Does anyone know if this is even possible using netcore DI and if it is what magic do i need to do to make it work? Thanks in advance
The text was updated successfully, but these errors were encountered: