Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap InstanceScope.Hybrid and IDisposable

I'm working on an asp.net-mvc 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()
{
    ForRequestedType<AetherDataContext>()
        .TheDefaultIs(() => new AetherDataContext())
        .CacheBy(InstanceScope.Hybrid);
}

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?

Update:

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);
    base.Dispose(disposing);
}

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

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.

like image 764
Simon Farrow Avatar asked Feb 02 '09 13:02

Simon Farrow


2 Answers

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

HttpContextBuildPolicy.DisposeAndClearAll();
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.

like image 115
Simon Farrow Avatar answered Sep 30 '22 17:09

Simon Farrow


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.

like image 31
Chris Marisic Avatar answered Sep 30 '22 18:09

Chris Marisic