ASP.NET C# 静态变量是全局的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET C# Static Variables are global?
提问by Dal
Today I released a small asp.net beta web application which allows internal staff to modify some product information. We started running into issues where users were overwriting each others product information... even though each staff member was editing a totally different row (Product).
今天我发布了一个小的asp.net beta web 应用程序,它允许内部人员修改一些产品信息。我们开始遇到用户覆盖彼此产品信息的问题……即使每个员工都在编辑完全不同的行(产品)。
After some searching on google, I think I know what is going on, its to do with the use of static variables, below is a quick rough example of the problem:
在谷歌上搜索后,我想我知道发生了什么,它与静态变量的使用有关,下面是问题的一个快速粗略示例:
// EditProductGroup.aspx.cs
public partial class EditProductGroup : System.Web.UI.Page
{
private static int _groupId = 0;
protected void Page_Load(object sender, EventArgs e)
{
_groupId = Convert.ToInt16(Request.QueryString["GroupID"]);
// get existing ProductGroup information from database using
//_groupId to find the row and populate textboxes
}
private void saveProductGroupData()
{
// when user hits 'save changes' button, update row
//where the primary key column 'ProductGroupID' matches _groupId in the table
}
}
So, according to my research, a static variable actually exists to the application as a whole, that means that if multiple users are using the application they will effectively all be reading the same 'value' for '_groupId' and in some cases actually setting it to a different value causing another users 'instance' of the page to save data to the wrong row (ProductGroupId).
因此,根据我的研究,整个应用程序实际上存在一个静态变量,这意味着如果多个用户正在使用该应用程序,他们将有效地读取 '_groupId' 的相同“值”,并且在某些情况下实际设置它为不同的值,导致页面的另一个用户“实例”将数据保存到错误的行 (ProductGroupId)。
My intention was that the static variable is isolated from other users, and should not intefere - each user has their own instance of the page thus their own instance of the '_groupId' variable.
我的意图是静态变量与其他用户隔离,并且不应干扰 - 每个用户都有自己的页面实例,因此他们自己的 '_groupId' 变量实例。
Luckily it was all taking place on a dev/staging database not the live db. I'm not sure whether I just need to drop off the 'static' keyword to stop the variable being set/read by everyone.
幸运的是,这一切都发生在开发/暂存数据库而不是实时数据库上。我不确定我是否只需要去掉 'static' 关键字来阻止每个人设置/读取变量。
Any thoughts? Thanks
有什么想法吗?谢谢
采纳答案by Adam Markowitz
Yes, in ASP.NET a static fields lifetime is for the app domain (note that this differs a bit for generic types).
是的,在 ASP.NET 中,静态字段生命周期是针对应用程序域的(请注意,这对于泛型类型略有不同)。
I'd recommend using the server Session for storage of data you want to associate with an instance of a client browser session (user login). i.e.
我建议使用服务器会话来存储要与客户端浏览器会话实例(用户登录)关联的数据。IE
Session["_groupID"] = Convert.ToInt16(Request.QueryString["GroupID"]);
you can retrieve it by doing:
您可以通过执行以下操作来检索它:
short groupID = Convert.ToInt16(Session["_groupID"]);
回答by Restuta
Static variables has the same lifetime as an application domain they were created in. So if you get some wrong values it can be some problems in the application logic or else. You should use SessionState for per-user data.
静态变量与创建它们的应用程序域具有相同的生命周期。因此,如果您得到一些错误的值,则可能是应用程序逻辑或其他方面的一些问题。您应该将 SessionState 用于每个用户的数据。
回答by LBushkin
You may want to take some time to familiarize yourself with what the static keyword is responsible for in C#. Read here: static in different languagesand MSDN here.
回答by JaredPar
It's incorrect to say that a static variable exists application wide. They in fact exist at two different levels
说静态变量存在于应用程序范围内是不正确的。它们实际上存在于两个不同的层次
- For non-generic types there is a single static variable per AppDomain
- For generic types there is one per generic instantiation per AppDomain
- 对于非泛型类型,每个 AppDomain 有一个静态变量
- 对于泛型类型,每个 AppDomain 的每个泛型实例化都有一个
An application can contain many AppDomains and is true in several types of applications.
一个应用程序可以包含许多 AppDomain,并且在多种类型的应用程序中都是如此。
If you want to store user specific settings though, use a Session variable.
如果您想存储用户特定的设置,请使用 Session 变量。