c# - StructureMap throws ArgumentNullException on HttpContext -
i'm having weird issue structuremap.mvc5
i created brand new mvc5 project in visual studio (default options asp.net mvc project left selected.)
i installed structuremap.mvc5 via nuget package manager (install-package structuremap.mvc
).
i added following code to top of "homecontroller.cs" file:
namespace testmvc.controllers { public interface itest { string testmessage(); } public class test : itest { public string testmessage() { return "this worked again 23"; } }
i added constructor , private member follows:
public class homecontroller : controller { private readonly itest _test; public homecontroller(itest test) { _test = test; }
finally, updated action result follows:
public actionresult about() { viewbag.message = _test.testmessage(); return view(); }
the project compiles , start up. given default index page normal, somewhere between 2 , 5 seconds after page returned in browser, exception thrown in "structuremapdependencyscope.cs" on return
line of method:
private httpcontextbase httpcontext { { var ctx = container.trygetinstance<httpcontextbase>(); return ctx ?? new httpcontextwrapper(system.web.httpcontext.current); } }
the exact error given is:
system.argumentnullexception unhandled user code hresult=-2147467261 message=value cannot null. parameter name: httpcontext paramname=httpcontext source=system.web stacktrace: @ system.web.httpcontextwrapper..ctor(httpcontext httpcontext) @ testmvc.dependencyresolution.structuremapdependencyscope.get_httpcontext() in d:\code\annies\anniesv4\anniesv4-bookingadministration\testmvc\dependencyresolution\structuremapdependencyscope.cs:line 69 @ testmvc.dependencyresolution.structuremapdependencyscope.get_currentnestedcontainer() in d:\code\annies\anniesv4\anniesv4-bookingadministration\testmvc\dependencyresolution\structuremapdependencyscope.cs:line 55 @ testmvc.dependencyresolution.structuremapdependencyscope.disposenestedcontainer() in d:\code\annies\anniesv4\anniesv4-bookingadministration\testmvc\dependencyresolution\structuremapdependencyscope.cs:line 90 @ testmvc.dependencyresolution.structuremapdependencyscope.dispose() in d:\code\annies\anniesv4\anniesv4-bookingadministration\testmvc\dependencyresolution\structuremapdependencyscope.cs:line 85 @ testmvc.app_start.structuremapmvc.end() in d:\code\annies\anniesv4\anniesv4-bookingadministration\testmvc\app_start\structuremapmvc.cs:line 44 innerexception:
checking, system.web.httpcontext.current
indeed null @ point.
if stop , restart project, same error occurs.
if press f5 continue, website continues function expected.
however, if, after pressing f5, wait few moments, stop , restart project, don't error again until make sort of code change , rebuild!
this seemingly makes 0 sense me!
anyways.. appreciated!
(using vs2015 enterprise rc if makes difference)
the problem in dispose of container. trying dispose nested container lives in httpcontext null in moment of disposal.
i made changes structuremapdependencyscope class in order avoid exception:
public icontainer currentnestedcontainer { { if (httpcontext == null) return null; return (icontainer)httpcontext.items[nestedcontainerkey]; } set { httpcontext.items[nestedcontainerkey] = value; } } private httpcontextbase httpcontext { { var ctx = container.trygetinstance<httpcontextbase>(); if (ctx == null && system.web.httpcontext.current == null) return null; return ctx ?? new httpcontextwrapper(system.web.httpcontext.current); } }
Comments
Post a Comment