【问题标题】:Unable to get child container from DependencyResolver in RequestContext无法从 RequestContext 中的 DependencyResolver 获取子容器
【发布时间】:2021-06-30 10:20:49
【问题描述】:

我在 ASP.Net Web API 中使用 Unity 作为我的依赖解析器。我使用了here的依赖注入教程。

当尝试使用在 BeginScope 中创建的子容器时,我得到的只是父容器。

我正在尝试通过以下代码在 ActionFilterAttribute 中使用它:

httpActionContext.RequestContext.Configuration.DependencyResolver

httpActionContext.ControllerContext.Configuration.DependencyResolver

在这两种情况下,它都返回父容器,而不是在调用 BeginScope 时为该特定请求创建的子容器。

如何让子容器解决依赖关系?这可能吗?

【问题讨论】:

    标签: c# asp.net-web-api unity-container


    【解决方案1】:

    看了source code后,发现容器保存在:

    request.Properties[HttpPropertyKeys.DependencyScope]
    

    我所要做的就是从这里得到它:

    var resolver = httpActionContext.Request.Properties[HttpPropertyKeys.DependencyScope] as IDependencyResolver;
    

    【讨论】:

      猜你喜欢
      • 2013-07-12
      • 2018-12-29
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多