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

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

问题描述

今天我发布了一个小的asp.net beta 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).

我的意图是将静态变量与其他用户隔离开来,不应干扰 - 每个用户都有自己的页面实例,因此有自己的_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.

幸运的是,这一切都发生在开发/临时数据库上,而不是实时数据库上.我不确定我是否只需要去掉 'static' 关键字来阻止每个人设置/读取变量.

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天全站免登陆