为什么应用程序变量数据在一段时间后丢失

本文关键字:一段时间 应用程序 变量 数据 为什么 | 更新日期: 2023-09-27 18:26:26

当我存储来自global.asax文件数据的数据时,我必须将数据存储在应用程序变量中。如果我正在为应用程序变量中的存储数据编写相同的代码,从一段时间后丢失的任何 ASPX 页面数据中存储数据,我想知道为什么会发生这种情况,请在下面建议是我的代码?

//storing data in application variable from aspx page
protected void getApplicatinVariable_Click(object sender, EventArgs e)
{
HttpContext.Current.Application[paramTypeId] = GetSelectedParameter(paramTypeId, flag);//fuction retruning string data
HttpContext.Current.ApplicationApplication["GroupUserListCache"] = CacheClass.GetGroupUserListCache();//get userlist
HttpContext.Current.ApplicationApplication["EquipListCache"] = CacheClass.GetAlarmEquipListCache();
}   
//from global.asax file in dis case data persist.
Application[paramTypeId] = CacheClass.GetSelectedParameter(paramTypeId, "");
Application["GroupUserListCache"] = CacheClass.GetGroupUserListCache();//get userlist
Application["EquipListCache"] = CacheClass.GetAlarmEquipListCache();//CacheClass.SetEquipment();

为什么应用程序变量数据在一段时间后丢失

根据Microsoft关于HttpApplicationState的文档,这是HttpContext.Current.Application的基本对象:

ASP.NET 应用程序是单个 Web 服务器上虚拟目录及其子目录范围内所有文件、页、处理程序、模块和代码的总和。

HttpApplicationState 类的单个实例是在客户端首次从特定 ASP.NET 应用程序虚拟目录中请求任何 URL 资源时创建的。为 Web 服务器上的每个 ASP.NET 应用程序创建一个单独的单个实例。然后通过内部应用程序对象公开对每个实例的引用。

应用程序状态不会在 Web 场(其中应用程序跨多个服务器承载(或 Web 园(其中应用程序跨同一台计算机上的多个进程承载(之间共享。

因此,从技术上讲,如果您在共享环境中,它不会持久化,但对于单个应用程序,它仅在第一个请求发生时启动该类的实例。