代码之家  ›  专栏  ›  技术社区  ›  MatthewMartin muthu

我要挂接什么事件来释放ASP.NET*请求*缓存中的IDisposable项?

  •  2
  • MatthewMartin muthu  · 技术社区  · 14 年前

    让我们想象一个班级

    class Foo: IDisposable
    {
     Dispose()
     {
       //Dispose of nonmanged resources.
     }
    }
    

    让一个用例存在的图像放入 HttpContext.Items . 当添加实现IDisposable的对象时,它不会自动引发错误(谁知道呢,也许答案是它应该这样做)

    我需要参加哪些活动才能处理该项目?

    我们还假设当对象被用于两个不同的方法块时,使用块不可用。

    1 回复  |  直到 13 年前
        1
  •  3
  •   Peter    13 年前

    每@jaroslav jandek,我想 Application_EndRequest 在global.asax中工作得很好。您可以做一个简单的检查,看看是否在 HttpContext.Items 如果是的话,就把它处理掉。

        protected virtual void Application_BeginRequest (Object sender, EventArgs e)
        {
            HttpContext.Current.Items["test"] = new IDisposableObject();
        }
    
        protected virtual void Application_EndRequest (Object sender, EventArgs e)
        {
            if(HttpContext.Current.Items.Contains("test")) {
                ((IDisposable)HttpContext.Current.Items["test"]).Dispose();
            }
    
        }