webadvanced / Structuremap.MVC5

Apache License 2.0
21 stars 9 forks source link

ArgumentNullException Parameter name: httpContext #2

Closed alexsaare closed 10 years ago

alexsaare commented 10 years ago

Locally this works as expected using IISExpress however, when deoployed to a Win2k8 R2 server (IIS 7) i'm getting an ArgumentNullException for httpContext in this method

private HttpContextBase HttpContext { get { var ctx = Container.TryGetInstance(); return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current); } }

Obviously System.Web.HttpContext is null but i'm not sure why. Any thoughts would be appreciated.

This happens for all requests. Full stack trace below (Company information sanitized)

***** UPDATE ***** This only seems to be a problem when built in release configuration.

Kind regards, Alex

[ArgumentNullException: Value cannot be null. Parameter name: httpContext] System.Web.HttpContextWrapper..ctor(HttpContext httpContext) +4992306 COMP.PROJ.Web.DependencyResolution.StructureMapDependencyScope.DoGetAllInstances(Type serviceType) +106 System.Web.Http.Services.DefaultServices.GetServices(Type serviceType) +321 System.Web.Http.ServicesExtensions.GetServices(ServicesContainer services) +62 COMP.PROJ.Web.DependencyResolution.IoC.Initialize() +127 COMP.PROJ.Web.App_Start.StructuremapMvc.Start() +11

[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +229 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +193 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35 WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +445 WebActivatorEx.ActivationManager.Run() +105

[InvalidOperationException: The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..] System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection1 methods, Func1 setHostingEnvironmentCultures) +12969195 System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +12968904 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +280 System.Web.Compilation.BuildManager.ExecutePreAppStart() +172 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1151

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12968244 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12807949

alexsaare commented 10 years ago

OK, so, the problem was that i was replacing the default filter provider in the IoC.Initialize using this

GlobalConfiguration.Configuration.Services.GetFilterProviders()

For some reason this tries to use StructureMapDependencyScope.HttpContext which fails as System.Web.HttpContex is null when built in release mode.

The fix was to move the code to remove the default filter provider to Global.asax Application_Start()

* please close this issue *

valmont commented 10 years ago

Glad this was resolved =)

android8 commented 9 years ago

I have the same problem when a WebAPI controller is called from a client. The WebAPI controllers use constructor injection to inject a repository dependency which should be resolved by StructureMap Ioc. In the Global.asax I don't have the GlobalConfiguration.Configuration.Services.GetFilterProviders(). Interestingly, the same code runs on my another development machine. Here is my stack trace. Thanks for your help.

System.ArgumentNullException was unhandled by user code HResult=-2147467261 Message=Value cannot be null. Parameter name: httpContext Source=System.Web ParamName=httpContext StackTrace: at System.Web.HttpContextWrapper..ctor(HttpContext httpContext) at WebApi2.DependencyResolution.StructureMapDependencyScope.get_HttpContext() in c:.........\WebApi2\DependencyResolution\StructureMapDependencyScope.cs:line 69 at WebApi2.DependencyResolution.StructureMapDependencyScope.get_CurrentNestedContainer() in c:.........\WebApi2\DependencyResolution\StructureMapDependencyScope.cs:line 55 at WebApi2.DependencyResolution.StructureMapDependencyScope.DisposeNestedContainer() in c:.........\WebApi2\DependencyResolution\StructureMapDependencyScope.cs:line 90 at WebApi2.DependencyResolution.StructureMapDependencyScope.Dispose() in c:.........\WebApi2\DependencyResolution\StructureMapDependencyScope.cs:line 85 at WebApi2.App_Start.StructuremapMvc.End() in c:.........\WebApi2\App_Start\StructuremapMvc.cs:line 44

bipin24x7 commented 7 years ago

Do we have the fix for this? I am seeing below exception when saving content in thread which is running in background.

Parameter name: httpContext System.ArgumentNullException: Value cannot be null. Parameter name: httpContext at System.Web.HttpContextWrapper..ctor(HttpContext httpContext) at Umbraco.Web.SingletonHttpContextAccessor.get_Value() at Umbraco.Web.RequestLifespanMessagesFactory.Get() at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.Publish(IContent content, Int32 userId)