ASP.NET C#静态变量是全球性的? [英] ASP.NET C# Static Variables are global?

查看:103
本文介绍了ASP.NET C#静态变量是全球性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我发布了一个小asp.net测试版的Web应用程序,允许内部人员修改某些产品信息。我们开始运行到哪里使用者覆盖其他产品信息......尽管每个工作人员在编一个完全不同的行(产品)的问题。

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).

在谷歌上搜索了一些,我想我知道是怎么回事,它的做用静态变量,下面是问题的一个快速粗略的例如:

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
    }
}

因此​​,根据我的研究,一个静态变量实际存在于该应用程序作为一个整体,这意味着,如果多个用户正在使用的应用中,它们将有效地全部为'_groupId'并且在一些读出同一'值'实际上的情况下将其设置为不同的值的页面将数据保存到错误的行(ProductGroupId)的另一种引起用户的实例。

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).

我的目的是,静态变量是从其它用户中分离,并且不应该intefere - 每个用户都有自己的页面上的实例因而自己的'_groupId'变量的实例

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.

幸运的是,这一切都发生在一个开发/临时数据库不活分贝。我不知道我是否只需要下车的静态关键字阻止变量被设置/每个人都读。

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.

有什么想法?谢谢

推荐答案

是的,在ASP.NET中的静态字段寿命的应用程序域(注意,这有些不同泛型类型)。

Yes, in ASP.NET a static fields lifetime is for the app domain (note that this differs a bit for generic types).

我推荐使用用于存储数据的要与客户端浏览器会话(用户登录)的实例关联的服务器会话。

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.

Session["_groupID"] = Convert.ToInt16(Request.QueryString["GroupID"]);

您可以通过检索它:

you can retrieve it by doing:

short groupID = Convert.ToInt16(Session["_groupID"]);

这篇关于ASP.NET C#静态变量是全球性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆