本文介绍了DryIOC和MediatR:IAsyncNotificationHandler和IAsyncRequestHandler都使用InResolutionScopeOf进行注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是我上一个问题的后续操作, DryIOC装饰器和InResolutionScopeOf

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

我想做的是在IAsyncRequestHandler和IAsyncNotificationHandler的解析范围内创建EF DbContext实例,这意味着注入请求中的上下文与注入通知中的上下文不能相同(从请求中发布) .由于通知是从请求处理程序内部发布的,因此此嵌套给我所需的设置带来了一些麻烦.

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.

值得注意的是,在给定的IAsyncRequestHandler或IAsyncNotificationHandler实例中注入的每个DbContext在其自己的装饰器上都必须相同.

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

我创建了一个dotnetfiddle,尝试进行设置 https://dotnetfiddle.net/KiFCHY. (在此示例中,我省略了装饰器)

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

它包含一个RequestHandler,当它被调用时它会打印一条消息,然后发布一个通知,该通知将打印另一条消息.但是,如您所见,由于MediatR无法获取IAsyncNotificationHandler实例(因为它无法解析DbContext),因此未调用该通知.

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).

可以进行此设置吗?

谢谢

推荐答案

找到了根本原因:ResolveMany<object>(serviceType)在MediatR设置中使用.

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

object标识您需要通过所需的运行时serviceType.但是DryIoc有一个问题,即使用服务类型object而不是必需的类型来查找匹配的分辨率范围.并且object绝对不能分配给IAsyncNotificationHandler<T>.

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>.

这是修改后的小提琴

敬请期待.我将使用修复版本更新答案.

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

此修复程序通过 DryIoc 2.9.2 发布.这是使用它的小提琴.感谢您提出和提出2个问题-实际用例最重要.

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.

这篇关于DryIOC和MediatR:IAsyncNotificationHandler和IAsyncRequestHandler都使用InResolutionScopeOf进行注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:44