I have a very strange problem with StructureMap.MVC5
I created a new MVC5 project in Visual Studio (the default options for the ASP.net MVC project remained selected.)
Then I installed structmap.mvc5 through the nuget package manager ( Install-Package StructureMap.MVC ).
Then I added the following code to the beginning of the "HomeController.cs" file:
namespace TestMVC.Controllers { public interface ITest { string TestMessage(); } public class Test : ITest { public string TestMessage() { return "this worked again 23"; } }
Then I added the constructor and private member as follows:
public class HomeController : Controller { private readonly ITest _test; public HomeController(ITest test) { _test = test; }
Finally, I updated the About Action action as follows:
public ActionResult About() { ViewBag.Message = _test.TestMessage(); return View(); }
The project is compiled and launched. I am assigned the default index page, as usual, but somewhere between 2 and 5 seconds after the page returns in the browser, I get an exception thrown in "StructureMapDependencyScope.cs" in the return line of this method:
private HttpContextBase HttpContext { get { var ctx = Container.TryGetInstance<HttpContextBase>(); return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current); } }
Exact error:
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:
Verification, System.Web.HttpContext.Current is actually zero at the moment.
If I stop and restart the project, the same error will occur.
If I press F5 to continue, the website will continue to work as expected.
However, if after pressing F5, I wait a few minutes, stop and restart the project, I will not get an error until I make some code and rebuild!
It seems completely useless to me!
Anyway .. any help would be appreciated!
(using VS2015 Enterprise RC, if that matters)