StructureMap 在 HttpContext 上抛出 ArgumentNullException

StructureMap throws ArgumentNullException on HttpContext

我在使用 StructureMap.MVC5

时遇到了一个非常奇怪的问题

我在 Visual Studio 中创建了一个全新的 MVC5 项目(选择了 ASP.net MVC 项目的默认选项。)

然后我通过 nuget 包管理器 (Install-Package StructureMap.MVC) 安装了 structuremap.mvc5。

然后我将以下代码添加到 "HomeController.cs" 文件的顶部:

namespace TestMVC.Controllers
{
    public interface ITest
    {
        string TestMessage();
    }
    public class Test : ITest
    {
        public string TestMessage()
        {
            return "this worked again 23";
        }
    }

然后我添加了一个构造函数和私有成员如下:

public class HomeController : Controller
{
    private readonly ITest _test;

    public HomeController(ITest test)
    {
        _test = test;
    }

最后,我更新了关于操作结果如下:

public ActionResult About()
{
    ViewBag.Message = _test.TestMessage();

    return View();
}

项目编译并启动。 我正常获得默认索引页面,但是在浏览器返回页面后的 2 到 5 秒之间,我在该方法的 return 行的 "StructureMapDependencyScope.cs" 中抛出异常:

private HttpContextBase HttpContext {
    get {
        var ctx = Container.TryGetInstance<HttpContextBase>();
        return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
    }
}

给出的确切错误是:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: httpContext
  ParamName=httpContext
  Source=System.Web
  StackTrace:
       at System.Web.HttpContextWrapper..ctor(HttpContext httpContext)
       at TestMVC.DependencyResolution.StructureMapDependencyScope.get_HttpContext() in d:\Code\Annies\AnniesV4\AnniesV4-BookingAdministration\TestMVC\DependencyResolution\StructureMapDependencyScope.cs:line 69
       at TestMVC.DependencyResolution.StructureMapDependencyScope.get_CurrentNestedContainer() in d:\Code\Annies\AnniesV4\AnniesV4-BookingAdministration\TestMVC\DependencyResolution\StructureMapDependencyScope.cs:line 55
       at TestMVC.DependencyResolution.StructureMapDependencyScope.DisposeNestedContainer() in d:\Code\Annies\AnniesV4\AnniesV4-BookingAdministration\TestMVC\DependencyResolution\StructureMapDependencyScope.cs:line 90
       at TestMVC.DependencyResolution.StructureMapDependencyScope.Dispose() in d:\Code\Annies\AnniesV4\AnniesV4-BookingAdministration\TestMVC\DependencyResolution\StructureMapDependencyScope.cs:line 85
       at TestMVC.App_Start.StructuremapMvc.End() in d:\Code\Annies\AnniesV4\AnniesV4-BookingAdministration\TestMVC\App_Start\StructuremapMvc.cs:line 44
  InnerException: 

检查,System.Web.HttpContext.Current此时确实为空

如果我停止并重新启动项目,会发生同样的错误。
如果我按 F5 继续,网站将继续按预期运行。
但是,如果在按 F5 后,我稍等片刻,停止并重新启动项目,在我进行某种代码更改和重建之前,我不会再次收到错误!

这对我来说似乎毫无意义!

无论如何..任何帮助将不胜感激!

(如果有任何区别,请使用 VS2015 Enterprise RC)

问题出在容器的处理上。正在尝试处置位于 HttpContext 中的嵌套容器,该容器在处置时为 null。

我对 StructureMapDependencyScope 进行了此更改 class 以避免此异常:

public IContainer CurrentNestedContainer
{
    get
    {
        if (HttpContext == null)
            return null;

        return (IContainer)HttpContext.Items[NestedContainerKey];
    }
    set
    {
        HttpContext.Items[NestedContainerKey] = value;
    }
}

private HttpContextBase HttpContext
{
    get
    {
        var ctx = Container.TryGetInstance<HttpContextBase>();
        if (ctx == null && System.Web.HttpContext.Current == null)
            return null;

        return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
    }
}