gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:49 25 4
gpt4 key购买 nike

当我存储来自 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's documentation HttpApplicationStateHttpContext.Current.Application 的基础对象:

An ASP.NET application is the sum of all files, pages, handlers, modules, and code within the scope of a virtual directory and its subdirectories on a single Web server.

A single instance of an HttpApplicationState class is created the first time a client requests any URL resource from within a particular ASP.NET application virtual directory. A separate single instance is created for each ASP.NET application on a Web server. A reference to each instance is then exposed via the intrinsic Application object.

Application state is not shared across either a Web farm (in which an application is hosted across multiple servers) or a Web garden (in which an application is hosted across multiple processes on the same computer).

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

关于c# - 为什么应用程序变量数据在一段时间后丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202292/

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