如何解决无法从 RequestContext 中的 DependencyResolver 获取子容器
我在 ASP.Net Web API 中使用 Unity 作为我的依赖项解析器。我使用了 here 中的依赖注入教程。
尝试使用在 BeginScope 中创建的子容器时,我得到的只是父容器。
我正在尝试使用以下代码在 ActionFilterAttribute 中使用它:
httpActionContext.RequestContext.Configuration.DependencyResolver
和
httpActionContext.ControllerContext.Configuration.DependencyResolver
在这两种情况下,它都返回父容器,而不是在调用 BeginScope 时为该特定请求创建的子容器。
如何让子容器解决依赖关系?这可能吗?
解决方法
查看 source code 后,我意识到容器保存在:
request.Properties[HttpPropertyKeys.DependencyScope]
我所要做的就是从这里获取它:
var resolver = httpActionContext.Request.Properties[HttpPropertyKeys.DependencyScope] as IDependencyResolver;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。