简体   繁体   English

Asp.Net中的静态变量行为

[英]Static variable Behaviour in Asp.Net

I have a OperationHelper class which is as follows: 我有一个OperationHelper类,如下所示:

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#. 所以想知道C#中Asp.Net中静态变量的行为。

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. 如果用户A将其设置为true,则用户B可以将该值获取为True还是具有不同的变量实例。

The behavior of static variables is that they are being created as soon as the code they belong to is reached. static变量的行为是,一旦到达它们所属的代码,就会立即创建它们。 To solve your problem, consider a static constructor for your class to properly initialize all values to your desire 要解决您的问题,请为您的类考虑一个static constructor ,以根据需要正确初始化所有值

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

    static OperationHelper() {
    //initialize your static variables here
    }
}

See here for a reference on static constructors. 有关static构造函数的参考,请参见此处

So want to know the behavior of the static variable in Asp.Net with C#. 所以想知道C#中Asp.Net中静态变量的行为。

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. 如果用户A将其设置为true,则用户B可以将该值获取为True还是具有不同的变量实例。

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. 因此,假设您有一个包含4个工作流程的池。

UserA ask for a page, Process 1 is replay and set a static value to A. UserA请求一个页面,正在重播进程1,并将静态值设置为A。
UserB ask for a page, Process 1 is replay and the static value is A. UserB请求一个页面,进程1正在重播,静态值为A。
UserA ask for a page, Process 2 is replay and the static value is not set. UserA请求页面,正在重播进程2,并且未设置静态值。

and so on. 等等。 More on the subject: Lifetime of ASP.NET Static Variable 有关此主题的更多信息: ASP.NET静态变量的生命周期
Where are static variables stored in asp.net aspx page 静态变量存储在asp.net aspx页中的哪里
Using static variables instead of Application state in ASP.NET 在ASP.NET中使用静态变量代替应用程序状态
Static methods on ASP.NET web sites ASP.NET网站上的静态方法
Asp.net static object appears sometimes as non global Asp.net静态对象有时显示为非全局对象

static variables are only created once. 静态变量仅创建一次。 so userB will get same instance of the variable to answer your question. 因此userB将获得该变量的相同实例来回答您的问题。

More on this have been discussed here . 这里已经讨论了更多。

您需要考虑使用会话,以便为每个访问该网站的用户提供不同的价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM