What you imagine makes sense - using the HTTP request context to store the material - but No, the one-time objects stored in the current HttpContext will not be automatically configured when the request is completed. One way or another, you have to do it.
There is an "End Request" event, which can be easily connected, for example, using the code that you go to Global.asax.cs. In the Application_EndRequest () method, you can call Dispose() manually for each object in the list that requires it.
One way to do this is to loop over each item in the context, check IDisposable, and then call Dispose if necessary.
protected void Application_EndRequest(Object sender, EventArgs e) { foreach (var key in HttpContext.Current.Items.Keys) { var disposable = HttpContext.Current.Items[key] as IDisposable; if (disposable != null) { disposable.Dispose(); HttpContext.Current.Items[key] = null; } } }
I think it should work out. ASPNET does not do this automatically for you. Of course, you need protection against exceptions, etc., before using this code in a real application.
Keith Craig from Vertigo wrote a related post a while ago , describing what you want to do as a template, in other words a way to do what needs to be repeated. It provides a class to help with this, to lazy load the DB context and pass it to the current context. There are some pitfalls with the approach - you can read about them in the discussion of the comment on this post. There are also a bunch of related articles cited in the comments.
Cheeso Mar 22 '10 at 11:15 2010-03-22 11:15
source share