StructureMap在HttpContext上抛出ArgumentNullException

Sk9*_*k93 7 c# structuremap asp.net-mvc structuremap3

我有一个非常奇怪的问题 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";
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

public class HomeController : Controller
{
    private readonly ITest _test;

    public HomeController(ITest test)
    {
        _test = test;
    }
Run Code Online (Sandbox Code Playgroud)

最后,我更新了About操作结果,如下所示:

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

    return View();
}
Run Code Online (Sandbox Code Playgroud)

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

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

给出的确切误差是:

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: 
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

Gui*_*non 7

问题在于处理容器.试图处理一个存在于HttpContext中的嵌套容器,该容器在处理时是空的.

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)