C# 多用户 ASP.NET Web 应用程序中静态变量的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14154892/
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
Scope of static Variable in multi-user ASP.NET web application
提问by Manas Saha
Does static variables retain their values across user sessions?
静态变量是否在用户会话中保留其值?
I have a ASP.NET web application where I have two buttons. One for setting the static variable value, another for Showing the static variable value.
我有一个 ASP.NET Web 应用程序,其中有两个按钮。一个用于设置静态变量值,另一个用于显示静态变量值。
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public static int customerID;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonSetCustomerID_Click(object sender, EventArgs e)
{
customerID = Convert.ToInt32(TextBox1.Text);
}
protected void ButtonGetCustomerID_Click(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(customerID);
}
}
}
While this works in single-user environment, What happens if there are 2 users simultaneously logged in from two computers, User 1 sets the value as 100, then User 2 sets the value as 200. after that user 1 invokes the Get Value button. What will he see as the value?
虽然这适用于单用户环境,但如果有 2 个用户同时从两台计算机登录会发生什么,用户 1 将值设置为 100,然后用户 2 将值设置为 200。在用户 1 调用“获取值”按钮之后。他会看到什么价值?
采纳答案by Icarus
Does static variables retain their values across user sessions?
静态变量是否在用户会话中保留其值?
Yes, that's why you should be VERY careful when you use static variables in a web app. You will run in concurrency issues as more than one thread servicing a request can modify the value of the variable.
是的,这就是为什么在 Web 应用程序中使用静态变量时应该非常小心的原因。您将遇到并发问题,因为为请求提供服务的多个线程可以修改变量的值。
While this works in single-user environment, What happens if there are 2 users simultaneously logged in from two computers, User 1 sets the value as 100, then User 2 sets the value as 200. after that user 1 invokes the Get Value button. What will he see as the value?
虽然这适用于单用户环境,但如果有 2 个用户同时从两台计算机登录会发生什么,用户 1 将值设置为 100,然后用户 2 将值设置为 200。在用户 1 调用“获取值”按钮之后。他会看到什么价值?
The user will see 200 afterwards.
之后用户将看到 200。
回答by andy
Static Variables Scope is Application Level.
静态变量范围是应用程序级别。
If you store something in Static variables, definitely your doing something wrong.
如果您将某些内容存储在静态变量中,那么您肯定做错了。
If one user saves the data(In Static variable), Same time another user access same page then he will get the same data(First User saved).
如果一个用户保存数据(在静态变量中),同时另一个用户访问同一个页面,那么他将获得相同的数据(第一个用户保存)。
So better you can store the values in **Sessions**.
回答by sunnysidedown916
This would work for you (keep in mind, you need to handle null values/-1):
这对您有用(请记住,您需要处理空值/-1):
public static int customerID
{
get { return session["customerID"] == null? -1 : (int)session["customerID"]; }
set { session["customerID"] = value; }
}
回答by heinz
Do not use static for the property then it works:
不要对属性使用静态,然后它就可以工作了:
public int customerID
{
get { return Session["customerID"] == null? -1 : (int)Session["customerID"]; }
set { Session["customerID"] = value; }
}