global.asax 的使用方法和范围(用于应用程序结束后的文件清理)
关于在 Web 服务器的文件系统上动态存储临时图像并处理其清理的主题:(在 .NET 3.5 中使用 C#)。
有人建议我使用 global.asax
文件来处理这个问题。
我只是不明白这东西是如何运作的。
我有两个单独的应用程序...
我发现 global.asax 应该位于网站的根目录中。
问题:
1)如何让 global.asax
仅针对这两个特定应用程序触发。
2)两个应用程序都需要创建一个字符串列表(文件位置),然后在应用程序终止时删除它们。 我是在应用程序中还是在 global.asax 中实例化此数组?
我的代码将如下所示:
List<string> fileLocations = new List<string>();
//I don't know where to put this.
//The next line of code will be in both applications (this could
//be called many times in one application session. The names of
//the files are generated from the clock (milliseconds and
//seconds combined, I might change this to use the actual
//random class combined with sessionID)
fileLocations.Add(file);
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
foreach(string file in fileLocations)
{
if(File.Exists(file))
File.Delete(file);
}
}
我对 global.asax 的实际工作原理感到困惑。 它像接口一样使用吗?
On the subject of dynamically storing temporary images and handling their cleanup on a web server's file system: (using C# in .NET 3.5).
It was suggested that I use a global.asax
file to handle this.
I just can't figure out how this thing works.
I have two separate applications...
I have figured out that the global.asax is supposed to be in the root directory of the website.
Questions:
1) How to I get the global.asax
to fire for only these two specific applications.
2) both applications need to create a list of strings (the file locations) then delete them on the application termination. Do I instantiate this array in the app, or in the global.asax
?
My code will look like this:
List<string> fileLocations = new List<string>();
//I don't know where to put this.
//The next line of code will be in both applications (this could
//be called many times in one application session. The names of
//the files are generated from the clock (milliseconds and
//seconds combined, I might change this to use the actual
//random class combined with sessionID)
fileLocations.Add(file);
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
foreach(string file in fileLocations)
{
if(File.Exists(file))
File.Delete(file);
}
}
I am confused about how the global.asax actually works. Is it used like an interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解如何使用 Global.asax 的一个好地方是阅读 ASP.NET 的使用快速入门。 每个网络应用程序/站点都有一个。 这有点像全球级别的活动。
请注意,在大多数服务器上,Application_End 事件不会经常触发。 仅当 IIS 应用程序池被卸载/回收、web.config 被修改、/Bin 中的程序集被更改或 Web 服务器停止的其他情况时,它才会触发。 在热门网站上,您的活动可能需要几周、几个月的时间才能触发。
A good place to look at how to use Global.asax is to read the ASP.NET Quickstart on it's usage. You have one per web application / site. It's kind of like Global level events.
Be aware, the Application_End event will not fire often, on most servers. It will only fire if the IIS app pool is unloaded/recyclyed, web.config modified , assemblies changed in /Bin,or someother situation where the webserver stops. On a popular site it could be weeks, months before your event ever fires.
MSDN 链接 和 维基百科链接 是起点。
它不像接口那样使用,更像是一个过滤器,它有一堆不同的钩子,用于启动应用程序对象、会话或请求等。 维基百科链接包含 Global.asax 可以进行的调用列表,但我建议先退后一步,了解 Web 应用程序与桌面应用程序相比是如何结束的。 IIS 可以回收 AppPool 作为结束应用程序的一种方法,尽管还有其他方法,因此如果该事件从未触发,则在应用程序端设置清理可能不是最好的主意。
MSDN Link and Wikipedia Link are places to start.
It isn't used like an interface, more like a filter that has a bunch of different hooks for things like starting the Application object or a Session or a Request. The Wikipedia link has a list of calls that the Global.asax can make though I would suggest stepping back for a moment to understand how a web application ends compared to a desktop application. IIS can recycle an AppPool as one way to end the application though there are others so setting a cleanup on application end may not be the best idea if that event never fires.