7

我有一个非常奇怪的问题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;
    }

最后,我更新了 About 动作结果如下:

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)

4

1 回答 1

10

问题在于容器的处置。正在尝试处理一个嵌套容器,该容器位于 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);
    }
}
于 2016-10-28T15:21:19.363 回答