StructureMap在HttpContext

时间:2015-06-04 10:31:29

标签: c# asp.net-mvc structuremap 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";
        }
    }

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

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)

1 个答案:

答案 0 :(得分: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);
    }
}