简体   繁体   English

在application_endrequest上部署对象上下文

[英]disposing object context on application_endrequest

I'm facing an issue with the EF4 object context which I store inside HttpContext.Current.Items , and then want to dispose as soon as the request is handled fully. 我遇到了EF4对象上下文的问题,我将其存储在HttpContext.Current.Items中 ,然后在完全处理请求后立即处理。

On Aplication_EndRequest event I call the Terminate() method of the RepositoryContext, which would find the active ObjectContext from the HttpContext.Current.Items collection, and call Close() on its connection and Dispose() on it. 在Aplication_EndRequest事件中,我调用RepositoryContext的Terminate()方法,该方法将从HttpContext.Current.Items集合中找到活动的ObjectContext ,并在其连接上调用Close()并在其上调用Dispose()

The problem is that sometimes I get weird behavior on one of my pages. 问题是,有时候我的某个页面会出现奇怪的行为。 On some occasions I get an error saying: 在某些情况下,我得到一个错误说:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection ObjectContext实例已被释放,不能再用于需要连接的操作

I thought that maybe this might happen since not only page requests call the Application_EndRequest event once they finish but also image requests and etc', and hence maybe sometimes other requests disposes the main page request ObjectContext before it is finished doing its job, but that should not happen since everything is made on the collection HttpContext.Current.Items which is of course not shared between HTTP requests. 我想也许这可能发生,因为不仅页面请求一旦完成就会调用Application_EndRequest事件,而且还会调用图像请求等等,因此有时其他请求可能会在完成其工作之前处理主页面请求ObjectContext,但这应该是因为一切都是在集合HttpContext.Current.Items上进行的,所以当然不能在HTTP请求之间共享。

Also, from research maybe it is caused due to lazy loading of some db requests, but this should not be the case here because I do not call Dispose anywhere else on code (I've checked) and hence Dispose() on the EndRequest should be called only when everything is finished, shouldn't it? 此外,从研究可能是由于一些数据库请求的延迟加载引起的,但这不应该是这里的情况,因为我没有在代码上的其他任何地方调用Dispose(我已经检查过),因此EndRequest上的Dispose()应该只有在一切都结束时才会被召唤,不应该吗?

Any ideas on what might cause this? 关于可能导致这种情况的任何想法? How can I test it? 我该怎么测试呢? what would you suggest? 你会建议什么?

Thanks! 谢谢!

This means that Dispose() was already called on the ObjectContext. 这意味着已经在ObjectContext上调用了Dispose()。 There are many different reasons as to why this is happening, but it boils down to the fact that something is calling Dispose() before Application_EndRequest. 关于为什么会发生这种情况有很多不同的原因,但它归结为在Application_EndRequest之前调用Dispose()的事实。 Without all of the source it would be impossible to tell exactly why. 没有所有的来源,就不可能确切地说出原因。

Because you are asking for recommendations, my first would be to take the ObjectContext out of HttpContext. 因为你要求建议,我的第一个是从HttpContext中取出ObjectContext。 The database connection should only live for a short time, and execute a specific task. 数据库连接应该只存在很短的时间,并执行特定的任务。 If it is short lived you can put the ObjectContext inside of a using statement which will automatically call Dispose() for you. 如果它是短暂的,您可以将ObjectContext放在using语句中,该语句将自动为您调用Dispose()。

Supposing you have a class that provides you the current ObjectContext, it's possible a programmer, following some examples from a blog or something like that, had written this: 假设您有一个为您提供当前ObjectContext的类,那么程序员可能会根据博客中的一些示例或类似内容编写此代码:

using(var context = ContextProvider.GetCurrentContext()){
    ...
}

and disposed the ObjectContext before the request ends. 并在请求结束之前处理了ObjectContext。

If you want to test where the ObjectContext is being disposed, you can do this: 如果要测试ObjectContext的处置位置,可以执行以下操作:

In your ObjectContext implementation, change the Dispose method to: 在ObjectContext实现中,将Dispose方法更改为:

public override void Dispose() {
    throw new InvalidOpearationException("Gotcha!");
}

public void ActuallyDisposePlease() {
    base.Dispose();
}

And in the Application_EndRequest call the ActuallyDisposePlease() method. 并在Application_EndRequest中调用ActuallyDisposePlease()方法。

OF COURSE, this is for testing/debugging/diagnosis perspective and NEVER should hit production. 当然,这是用于测试/调试/诊断的角度,并且永远不应该投入生产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM