在Asp.Net静态变量行为 [英] Static variable Behaviour in Asp.Net

查看:162
本文介绍了在Asp.Net静态变量行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OperationHelper类,这是如下:

 公共类OperationHelper
    {
        ///<总结>
        ///获取或设置添加操作价值
        ///< /总结>
        公共静态布尔AddOperation {搞定;组; }        ///<总结>
        ///获取或设置编辑操作价值
        ///< /总结>
        公共静态布尔EditOperation {搞定;组; }        ///<总结>
        ///获取或设置删除操作价值
        ///< /总结>
        公共静态布尔DeleteOperation {搞定;组; }        ///<总结>
        ///获取或设置选择操作价值
        ///< /总结>
        公共静态布尔SelectOperation {搞定;组; }
    }

在每次请求这个值被重新分配。当我运行这个地方它工作正常。
但是当我发表了code一些值不会分配或不工作,因为它应该工作。

所以想知道静态变量在Asp.Net用C#的行为。

是静态变量为全局变量,这是所有用户的访问?如果用户A向真,用户B设置获取值True或它的变量不同的实例。


解决方案

  

所以,想知道在Asp.Net的静态变量的行为
  C#。


  
  

是静态变量为全局变量是访问
  所有的用户?如果用户A向真,用户B设置获取价值
  True或它具有可变的不同实例。


的行为是一样,只有当您的您池上运行在一个工作流程您的网站。

如果您有游泳池的多个工作进程,那么每个进程都有自己的静态值,然后是您不知道的东西的过程是给每个请求,给每个用户。和工艺一起他们没有沟通。

因此​​,让我们说,你有4个工作过程的游泳池。

用户A要求一个页面,处理1是回放和设置静态值A.结果
用户B请求一个页面,进程1重播,静态值是A.结果
用户A请求一个页面,处理2回放和静态值未设置。

和等。
更多关于这个问题的:
<一href=\"http://stackoverflow.com/questions/8919095/lifetime-of-asp-net-static-variable/8919336#8919336\">Lifetime ASP.NET的静态变量结果
<一href=\"http://stackoverflow.com/questions/5559529/where-are-static-variables-stored-in-asp-net-aspx-page/5559554#5559554\">Where是存储在asp.net aspx页面结果静态变量
<一href=\"http://stackoverflow.com/questions/10960695/using-static-variables-instead-of-application-state-in-asp-net/10964038#10964038\">Using静态变量而不是应用程序状态在ASP.NET 结果
<一href=\"http://stackoverflow.com/questions/2779617/static-methods-on-asp-net-web-sites/2779646#2779646\">Static在ASP.NET网站结果的方法
<一href=\"http://stackoverflow.com/questions/15721883/asp-net-static-object-appears-sometimes-as-non-global/15722751#15722751\">Asp.net静态对象有时会出现非全球

I have a OperationHelper class which is as follows:

public class OperationHelper
    {
        /// <summary>
        /// Gets or sets the Add Operation value
        /// </summary>
        public static bool AddOperation { get; set; }

        /// <summary>
        /// Gets or sets the Edit Operation value
        /// </summary>
        public static bool EditOperation { get; set; }

        /// <summary>
        /// Gets or sets the Delete Operation value
        /// </summary>
        public static bool DeleteOperation { get; set; }

        /// <summary>
        /// Gets or sets the Select Operation value
        /// </summary>
        public static bool SelectOperation { get; set; }
    }

on each request this values are re-assigned. when i run this locally it works properly. but when i published the code some values are not get assigned or not working as it should work.

So want to know the behavior of the static variable in Asp.Net with C#.

are static variable equal to global variable which is accessible to all user? If it is set by user A to true can user B get that value as True or it has different instance of the variable.

解决方案

So want to know the behavior of the static variable in Asp.Net with C#.

are static variable equal to global variable which is accessible to all user? If it is set by user A to true can user B get that value as True or it has different instance of the variable.

The behavior is like that only if your run your site under one working process on your pool.

If your pool have more than one working process, then each process have their static values, and is unknown to you what process is given to each request, to each user. And process together they are not communicate.

So let say that you have a pool with 4 working process.

UserA ask for a page, Process 1 is replay and set a static value to A.
UserB ask for a page, Process 1 is replay and the static value is A.
UserA ask for a page, Process 2 is replay and the static value is not set.

and so on. More on the subject: Lifetime of ASP.NET Static Variable
Where are static variables stored in asp.net aspx page
Using static variables instead of Application state in ASP.NET
Static methods on ASP.NET web sites
Asp.net static object appears sometimes as non global

这篇关于在Asp.Net静态变量行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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