Search code examples
c#structuremaphttpcontext

How to release and dispose all Http-scoped objects on Container instance?


In StructureMap, how can I release and dispose al Http-scoped objects on a specific Container instance? For the default intance in Object Factory, I can execute the method ReleaseAndDisposeAllHttpScopedObjects(), but the Container class and the IContainer interface doesn't seem to have such method.


Solution

  • If you look at the internals of ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects, you can see that it is a conveniency method implemented like this:

    public static void ReleaseAndDisposeAllHttpScopedObjects()
    {
        HttpContextLifecycle.DisposeAndClearAll();
    }
    

    IE. You can invoke the HttpContextLifecycle.DisposeAndClearAll method to clear the objects.

    Edit: Since the HttpContextLifecycle is global and not per container, I think that a nested container approach would be the solution to gain more fine grained control over the object lifetime during a Http Request.