DryIOC and MediatR: Injection using InResolutionSc

2019-07-30 10:07发布

This question is a follow up to my previous question, DryIOC Decorator and InResolutionScopeOf

What I'm trying to do is create EF DbContext instances in the resolution scope of both IAsyncRequestHandler and IAsyncNotificationHandler, meaning the context injected in a request can't be the same as one injected in a notification (published from a request). Since the notifications are published from inside the request handlers, this nesting is creating some troubles with my desired setup.

It is worth noting that each DbContext injected in a given IAsyncRequestHandler or IAsyncNotificationHandler instance needs to be the same across their own decorators.

I've created a dotnetfiddle with my attempt at setting this up https://dotnetfiddle.net/KiFCHY. (I've ommitted decorators in this example)

It contains a RequestHandler which prints a message when it is called, and it then publishes a notification, which prints another message. However, as you can see, the notification isn't called because MediatR cannot get the IAsyncNotificationHandler instance (because it can't resolve the DbContext).

Is this setup possible?

Thanks

1条回答
贪生不怕死
2楼-- · 2019-07-30 10:47

Found the root cause: ResolveMany<object>(serviceType) which is used in MediatR setup.

An object identifies that you need to pass run-time required serviceType. But DryIoc has an issue of using service type object instead of required type to find the matching resolution scope. And an object is definitely not assignable to IAsyncNotificationHandler<T>.

Here is the modified fiddle

Stay tuned for the fix. I will update my answer with the fix version.

Updated with fix version

The fix is released with DryIoc 2.9.2. Here is fiddle which uses it. Thanks for asking and surfacing 2 issues - real use cases matter the most.

查看更多
登录 后发表回答