I'm using Generic Type Requests and Handlers.
If I have more than one IAsyncRequestHandler DryIoc is resolving multiple instances, instead of a single instance, causing MediatR to throw an exception.
How can I solve this problem?
Please check this behavior at Net Fiddle here.
(Note that in this example Generic Type is only used to prove the point - MediatR throwing the exception - In my production code, Generic Types are actually needed)

- 240
- 1
- 2
- 15
-
Played a bit with your test - btw thanks for providing. I have a feeling that may be something on DryIoc side, that RegisterMany does not register not closed nested generic type. But that is strange, why it was not found before. Setup is quite simple. I will debug and come back with more info. – dadhi Feb 14 '17 at 19:17
-
Thanks, dadhi. I guess Generic Type Requests and Handlers are not very common :) Maybe that's the reason it hasn't showed up before. – DMVC Feb 15 '17 at 08:47
-
The issue is reproducible, but for now no clue why. Did you tried to check on MediatR v3 ? – dadhi Feb 15 '17 at 14:01
-
Using MediatR 3.0.0 it throws a different exception. It's trying to resolve a IRequestHandler instead of a IAsyncRequestHandler (weird stuff). Check it out [here](https://dotnetfiddle.net/Nn2Bxv). I've already posted a question regarding this issue [here](http://stackoverflow.com/questions/42177973/dryioc-with-mediatr-iasyncrequesthandler-resolve-exception). The exception on my VS 2015 is "Unable to resolve MediatR.IRequestHandler
, String>. Where no service registrations found and number of Rules.FallbackContainers:0 and number of Rules.UnknownServiceResolvers:0". – DMVC Feb 15 '17 at 19:14 -
The full picture is now clear. It seems that's you found new DryIoc bug with open generics. Fixing now. – dadhi Feb 15 '17 at 21:22
1 Answers
The fix for your problem is out with DryIoc 2.10.1.
The reason details are in issue #446.
Update:
Regarding sample with class GoodMorningRequestHandler<T> : IAsyncRequestHandler<GoodMorningRequest, string>
the exception is bit misleading but it is here as expected.
Reason is that above mapping is not registered at all by DryIoc RegisterMany
. It happens because RegisterMany
checks that service type (IAsyncRequestHandler<GoodMorningRequest, string>
) should supply open-generic parameter T
for implementation GoodMorningRequestHandler<T>
, and obviously it cannot do that. Therefore this service type is filtered out.
After that, the remaining implementation would be HelloRequestHandler<T>
which does not match to resolved IAsyncRequestHandler<GoodMorningRequest, string>
, so the exception.
To confirm for yourself try to just register explicitly:
container.Register(typeof(IAsyncRequestHandler<GoodMorningRequest, string>), typeof(GoodMorningRequestHandler<>))
. It will throw the exception. In comparison RegisterMany
designed to be more tolerant, therefore it will just skip this pair.

- 4,807
- 19
- 25
-
When all MediatR requests are Generic Typed, your fix is able to resolve all IAsyncRequestHandlers. As far as my tests concern, the problem arises when you have regular Requests with Generic Typed IAsyncRequestHandlers. Please check a sample [here](https://dotnetfiddle.net/BeAtMd) – DMVC Feb 18 '17 at 16:04
-
that's annoying, seems like I messed it up at the end. will re-check and push a new version. – dadhi Feb 18 '17 at 17:37
-
I deduced it was something like that, beacuse the RequestHandler was not being registered. Anyway, I've included the generic type in my Request even though I don't actually need it, and everything works just fine. Thanks and keep up the good work. – DMVC Feb 18 '17 at 23:49