Skip to content

Order of EndRequest events #5

@MelGrubb

Description

@MelGrubb

I appreciate the fact that the StructureMap.MVC package now gives us a container-per-request pattern out of the box, but there is a problem with the implementation of StructureMapScopeModule. Because its Init method is hit so early in the lifecycle of the application, its subscription to the EndRequest method is the first one registered, trumping all others. In itself, this isn't a problem, but it means that the nested container gets disposed before the Application_EndRequest in Global.asax gets called. If you needed anything out of the container in that method, you'll just get a null reference exception because the container is long gone by then.

It is possible to work around this by using the main Container for stuff that happens in Global.asax, but in the case of things like logging something to a database, you'd now hold a database reference at the main container level, defeating the benefit of the nested container.

I could move the nested container disposal to Global.asax, and control the lifecycle from there, but that means individually and carefully merging any future updates to the Structuremap.MVC5 NuGet package, and re-applying this move.

Moving the nested container handling into a module does a nice job of making it "free" and invisible to developers, but causes problems in situations like this, which I don't think will be that uncommon. People want to clean things up in EndRequest. Often that will require items from the container. Hopefully you can find an approach that works here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions