我正在使用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
中释放它是必需的吗?如果不这样做会发生什么?