Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentNullException: HttpContext #7

Open
cocowalla opened this issue Oct 24, 2014 · 14 comments
Open

ArgumentNullException: HttpContext #7

cocowalla opened this issue Oct 24, 2014 · 14 comments

Comments

@cocowalla
Copy link

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
Copy link

Ef0Dev commented Nov 4, 2014

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
Copy link
Author

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

@Ef0Dev
Copy link

Ef0Dev commented Nov 5, 2014

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
Copy link
Author

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

txavier added a commit to txavier/Structuremap.MVC5 that referenced this issue Nov 28, 2014
Fixes issue with context being null.

webadvanced#7
txavier added a commit to txavier/Structuremap.MVC5 that referenced this issue Nov 28, 2014
Updated fix to the context argument null issue described in the following issue report.

webadvanced#7
@Ef0Dev
Copy link

Ef0Dev commented Jan 5, 2015

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

@vgaltes
Copy link

vgaltes commented Feb 4, 2015

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
Copy link

I'm also having the same problem.

1 similar comment
@alvpaz
Copy link

alvpaz commented Mar 26, 2015

I'm also having the same problem.

@vgaltes
Copy link

vgaltes commented Mar 26, 2015

Hi,

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

#9

Cheers,
Vicenç

@khandokar
Copy link

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
Copy link

Still an issue.

@navnitmehta
Copy link

navnitmehta commented Nov 10, 2016

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
Copy link

rekna1 commented Nov 27, 2017

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
Copy link

Same issue here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants