StructureMap InstanceScope.Hybrid and IDisposable


I'm working on an application. The linq data context is being passed into my service objects by structure map. I've got is set to have a scope of hybrid. This all works just fine.

protected override void configure()
        .TheDefaultIs(() => new AetherDataContext())

The problem is that I keep running our of memory, I'm wondering if the IDisposable interface is ever actually being called.

Anyone got any ideas?

Failing that anyone got any other idea for things that might be causing my memory exceptions?


So some additional information, I just stuffed a couple of methods into my data context an put brake points in there.

protected override void Dispose(bool disposing)
    Debug.WriteLine("Disposing: " + DateTime.Now);

public new void Dispose()
    Debug.WriteLine("Disposing: " + DateTime.Now);

I'm not quite sure that I'm doing this the correct way, I'm guessing that the new method will be called?

Anyway, neither of the brake points were hit. However the constructor for the same class was called on every request though. Not ideal I'm thinking.


This is almost an exact copy of the question I asked 2 days ago: Session containing items implementing IDisposable

InstanceScope.Hybrid just stores the object inside HttpContext.Current.Items if it exists or ThreadLocal storage otherwise and InstanceScope.HttpSession works the same way other than it uses the HttpSession and ThreadLocal. The items collection lives per request, so if you implement the pattern pointed out on my question you should see Dispose firing at the end of the current request.

So the solution; its Cassini causing the problems. Basically it creates a new context for every request. Which is why I was seeing the context be created over again, as to why it wasn't calling I disposable properly I no idea. But again I'm prepared to believe that this is something to do with Cassini.

Ok so the latest version of StructureMap (2.3.5) has a useful little method called

Cleanup convenience methods on HttpContext and ThreadLocal. HttpContextBuildPolicy.DisposeAndClearAll(), ThreadLocalStoragePolicy.DisposeAndClearAll(). Calling either method will eject all cached instances and call IDispose if the object is IDisposable.

Previously the dispose methods weren't being called called, I added that to Application_EndRequest and they are now. I'm hoping that this will solve some of my memory problems.

We shall see.

This video can help you solving your question :)
By: admin