[英]What happens if you don't dispose HttpContext.Current.Items?
我正在使用HttpContext.Current.Items
根据登录用户设置租户。 为此,我使用以下代码:
protected virtual void Application_BeginRequest()
{
HttpContext.Current.Items["_CurrentTenant"] = Tenant.GetCurrent();
}
protected virtual void Application_EndRequest()
{
var currentTenant = HttpContext.Current.Items["_CurrentTenant"] as Tenant;
if (currentTenant != null)
currentTenant.Dispose();
}
由于HttpContext.Current.Items
只存在于当前请求中,我希望在请求完成后将其销毁。 此外, CurrentTenant
对象是完全托管的对象。
因此,是否需要在EndRequest
处置? 如果你不那样做会怎么样?
因此,是否需要在EndRequest中处置?
只有您可以知道,在请求结束时处置对象是否是您想要的。 确保在不再使用时处理掉。 如果那是在请求的最后,那么这可能是一个好地方。
如果你不那样做会怎么样?
不会调用.Dispose()
方法。 这对你的特定课程意味着什么只是你可以说的。 正常模式会在终结器中调用.Dispose()
,但在此之前,它可能需要一段时间。 在那段时间发生的事情完全取决于为什么它首先被标记为IDisposible
。 也许文件没有关闭,连接可能保持打开,句柄可能不会被返回...只有你可以知道它是否是你的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.