views:

30

answers:

1

I have to share ObjectContext (from EF4) between many objects, so i'm creating one Context per one HttpRequest (i haven't found better way to do this), but there is little problem...

When i use

using (ObjectContext ctx = new ...) {}

it always disposes context after closing bracket. So how should i dealt with it when my context lives for HttpRequest? Can i call any event after HttpRequest ends? or event when response was sent?

Not disposing context may cause errors in this situation when one context is created per HttpRequest?

+1  A: 

Sure, there's an EndRequest event.

Note that most DI frameworks will do this for you, automatically. But if you'd prefer to do it yourself, you can handle that event in global.asax.cs.

Craig Stuntz