gpt4 book ai didi

asp.net - 我应该 Hook 什么事件来处理 ASP.NET *Request* 缓存中的 IDisposable 项目?

转载 作者:行者123 更新时间:2023-12-04 06:50:12 26 4
gpt4 key购买 nike

让我们想象一个类(class)

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

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

我需要 Hook 什么事件来处理该项目?

我们还假设 using 块不可用,因为对象被用于两个不同的方法块。

最佳答案

每@Jaroslav Jandek,我认为 Hook 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();
}

}

关于asp.net - 我应该 Hook 什么事件来处理 ASP.NET *Request* 缓存中的 IDisposable 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3240030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com