webadvanced / Structuremap.MVC5

Apache License 2.0
21 stars 9 forks source link

ArgumentNullException: HttpContext #7

Open cocowalla opened 9 years ago

cocowalla commented 9 years ago

When I first start debugging a project using IIS Express I get an ArgumentNullException in StructureMapDependencyScope.HttpContext, as System.Web.HttpContext.Current is null.

I'm not sure, but this might be because I'm trying to resolve dependencies in Application_Start for use in authorisation filters by calling DependencyResolver.Current.GetService<> - maybe this is too early for the HttpContext to exist?

In any case, it means that StructureMap is not created HTTP-scoped instances; I can end up with multiple instances of my DbContext, for example, despite the fact it should be HTTP-scoped.

Ef0Dev commented 9 years ago

I get the same exception, but seemingly randomly (at least I haven't been able to narrow it down yet - I'm sure there is something), and it happens for me with varying frequency. However, I am not touching StructureMap at all in Global.asax. The only thing I've done is manually configure a couple of dependencies in DefaultRegistry.cs.

I did note the other closed issue for the same exception, but my scenario at least is different as it happens in debug mode and the only SM-related code I've touched is DefaultRegistry.cs.

cocowalla commented 9 years ago

Actually, I don't get this error every time either.

Ef0Dev commented 9 years ago

So I have found one instance where, for me, it's guaranteed to happen. I cannot confirm that it's isolated to this alone, but it may be.

It happens whenever I check something in or out of TFS while running in debug mode locally on IIS Express. It happens right when I click "Check In" or "Check Out for Edit...".

It happens regardless of the files involved in the check-in or check-out. Could be some random NuGet package update, or a change to a random service class. For example, I just checked out my ErrorController, clicked run, and then tried to check it back in. Boom - exception.

If I continue through the exception, things seem to run normally. It never hits my global error handling filter, either.

The exception occurs on this line of StructureMapDependencyScope.cs (for me it's line 69): return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);

sm_mvc5_exception

If I think about what happens when I check files into or out of TFS, the only thing I can think of is them changing to and from Read-only. But these files don't seem to have anything to do with StructureMap. And they're fine when they're checked out, or when they're checked in... it's only the act of changing the state.

Now obviously lots of other non-SM code uses the HttpContext - but I only get ArgumentNull exceptions in this SM file.

@cocowalla - can you tie your issue back to a scenario similar to mine? Or is yours different?

cocowalla commented 9 years ago

@Ef0Dev no, didn't notice anything related to TFS It would seem unlikely it's related; probably just a coincidence

Ef0Dev commented 9 years ago

@txavier's changes solve the issue for me at least. Thanks txavier!

vgaltes commented 9 years ago

Hi,

I'm having an exception when StructureMapDependencyScope.Dispose() is called from the ApplicationShutdownMethod (ASP.NET MVC 5). The problem is related to this discussion because the exception is being thrown in the HttpContext property of the StructureMapDependencyScope class. I've solved it changing slightly the code:

private HttpContextBase HttpContext {
    get {
        return (System.Web.HttpContext.Current == null
            ? null
            : (Container.TryGetInstance<HttpContextBase>() ??
                new HttpContextWrapper(System.Web.HttpContext.Current)));
    }
}

Is this code correct? Do you want me to make a PR?

Thanks in advance,

andersonaap commented 9 years ago

I'm also having the same problem.

alvpaz commented 9 years ago

I'm also having the same problem.

vgaltes commented 9 years ago

Hi,

I created a pull request to fix this problem. You can see it here:

https://github.com/webadvanced/Structuremap.MVC5/pull/9

Cheers, Vicenç

khandokar commented 8 years ago

Still,in package manager control,it is not handled https://www.nuget.org/packages/StructureMap.MVC5/ . Any one install the package will face the problem

dotnetchris commented 8 years ago

Still an issue.

navnitmehta commented 7 years ago

I am dealing with the same issue, seems like its related to usage of async pattern & when there is something significant to process (which will put this thread back into pool) and the continuation is on another thread at this point HttoContext.Current is null. Suggested fix is to have httpRuntime targetFramework="4.5" in web.config more explanation here https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/

rekna1 commented 6 years ago

What's the status of this issue ? I also have the problem. E.g. when you recycle the project, the exception is thrown. Is the suggested solution from txavier the right solution ? Or is there a reason why this is not commited to the main trunk ?

nicolastintorre commented 5 years ago

Same issue here