views:

1502

answers:

3

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

 public new void Dispose()
 {
  Debug.WriteLine("Disposing: " + DateTime.Now.ToString());
  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.

A: 

This is almost an exact copy of the question I asked 2 days ago: http://stackoverflow.com/questions/498095/session-containing-items-implementing-idisposable

InstanceScope.Hybrid just stores the object instead 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 Dispose firing at the end of the current request.

Chris Marisic
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
Except it doesn't, I checked. I've found an a solution and stuck it in below.
Simon Farrow
turns out I was wrong, I'm a Muppet my apologies
Simon Farrow
+2  A: 

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
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
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
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
A: 

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