Static variable Behaviour in Asp.Net

2019-09-18 15:00发布

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.

4条回答
孤傲高冷的网名
2楼-- · 2019-09-18 15:54

static variables are only created once. so userB will get same instance of the variable to answer your question.

More on this have been discussed here.

查看更多
We Are One
3楼-- · 2019-09-18 15:58

you need to consider session that will give you different value for each user accessing the site

查看更多
Ridiculous、
4楼-- · 2019-09-18 15:59

The behavior of static variables is that they are being created as soon as the code they belong to is reached. To solve your problem, consider a static constructor for your class to properly initialize all values to your desire

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.

查看更多
一夜七次
5楼-- · 2019-09-18 16:00

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

查看更多
登录 后发表回答