Guillermo Gutiérrez
Guillermo Gutiérrez

Reputation: 17819

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.

Upvotes: 5

Views: 1856

Answers (1)

PHeiberg
PHeiberg

Reputation: 29811

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.

Upvotes: 6

Related Questions