7

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
Simon Farrow
  • 1,901
  • 2
  • 20
  • 26
  • I've always wondered what happens to IDisposables and the various caching policies. Failing finding anything in the docs, stick breakpoints in your Dispose() and see when it happens (if at all). I'd be interested in the responses to this – Andrew Bullock Feb 02 '09 at 13:46
  • what solution did you end up going with? i have a current similar concern: http://stackoverflow.com/questions/3665336/iqueryable-repository-with-structuremap-ioc-how-do-i-implement-idisposable – RPM1984 Sep 08 '10 at 09:20

3 Answers3

4

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.

Simon Farrow
  • 1,901
  • 2
  • 20
  • 26
  • I'd be concerned about having this in Application_EndRequest since HttpContext doesn't exist in global.asax this will be firing every single page request. I see huge potential for concurrency issues, not to mention I don't think it would actually work correctly since the HttpContext doesn't exist. – Chris Marisic Feb 02 '09 at 15:57
  • The problem that I'm having is that an instance of the DataContext is being created for every request. I can either get rid of that on every request. Or stop recreating them, any ideas? – Simon Farrow Feb 03 '09 at 14:41
  • 1
    If you use InstanceScope.Hybrid it will create a new context for every request. That's the intention of Hybrid to cache your object per request. If you want it to last longer look at HttpSession or Singleton but be careful with keeping the datacontext around longer I'm not sure of the implications – Chris Marisic Feb 05 '09 at 14:50
  • Hello, it seems that the method call mentioned here has been removed in the current StructureMap (2.6.3) and has been replaced by this: ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); – Nikolaos Georgiou Feb 18 '12 at 14:18
1

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.

Community
  • 1
  • 1
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • I'm specifically interested in what structure map is doing with the classes, not what the surrounding application does at the end of its lifetime. If I'm wrong please feel free to correct me. – Simon Farrow Feb 02 '09 at 14:58
  • Except it doesn't, I checked. I've found an a solution and stuck it in below. – Simon Farrow Feb 02 '09 at 15:21
0

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.

Simon Farrow
  • 1,901
  • 2
  • 20
  • 26