简体   繁体   English

静态变量ASP.NET

[英]static variable ASP.NET

in asp.net web application if I want to store variable in a static object is that right? 在asp.net Web应用程序中,如果我想将变量存储在静态对象中是吗? I don't want that this object will share its value with another request. 我不希望该对象与另一个请求共享其值。

public static object Objects
{
    get
    {
        if (HttpContext.Current.Items["Objects"] != null)
            return (object)HttpContext.Current.Items["Objects"];
        else
        {
            HttpContext.Current.Items["Objects"] = new object();
            return new object();
        }
    }

    set { HttpContext.Current.Items["Objects"] = value; }
}

THX 谢谢

Static fields are shared across the whole AppDomain. 静态字段在整个AppDomain中共享。 This means that ALL requests in a ASP.NET web application will use the same value and you will have to make sure that variable is thread-safe. 这意味着ASP.NET Web应用程序中的所有请求都将使用相同的值,并且您必须确保该变量是线程安全的。 If this is not what you want, consider the following: 如果这不是您想要的,请考虑以下因素:

  • Storing the value in the user session: HttpContext.Current.Session 在用户会话中存储值: HttpContext.Current.Session
  • Storing the value in the request: HttpContext.Current.Items . 将值存储在请求中: HttpContext.Current.Items This way the value is cached throughout the current request, but not shared across requests. 这样,该值将在整个当前请求中缓存,但不会在请求之间共享。
  • Don't store the value at all. 根本不存储值。

In your case however, you are using a static property. 但是,就您而言,您正在使用static属性。 This static property maps in your case to the HttpContext.Current.Items , which means that each request automatically gets its own variable and variables are not shared. 在您的情况下,此静态属性映射到HttpContext.Current.Items ,这意味着每个请求都会自动获取其自己的变量,并且变量不会共享。

In other words, your code is thread-safe. 换句话说,您的代码是线程安全的。

I don't want that this object will share its value with another request. 我不希望该对象与另一个请求共享其值。

You should not make it static then. 那你不应该把它设为静态。

If you want to use a static variable without sharing it across all requests you could store a session variable in a static property. 如果要使用静态变量而不在所有请求之间共享它,则可以将会话变量存储在静态属性中。 Use HttpContext.Session to access it. 使用HttpContext.Session进行访问。

public static object Objects  
{
    get
    {
        if (HttpContext.Current.Session["Objects"] != null)
            return (object)HttpContext.Current.Session["Objects"];
        else
        {
            var obj = new object();
            HttpContext.Current.Session["Objects"] = obj;
            return obj;
        }
    }

    set { HttpContext.Current.Session["Objects"] = value; }
}

However, you should not return object but the correct type, that will increase readability, prevents exceptions and avoids always casting it where you use it. 但是,您不应返回object而应返回正确的类型,这将增加可读性,防止异常并避免始终将其强制转换为使用对象的位置。

There are 4 main server Dictionary objects that store values between requests. 有4个主服务器Dictionary对象,用于存储请求之间的值。 Application - shares values between all requests and users. 应用程序-在所有请求和用户之间共享值。 Cache - shares values between all requests and users but can be invalidated without restarting application. 缓存-在所有请求和用户之间共享值,但是可以在不重新启动应用程序的情况下使其无效。 Session - shares values between requests within the same session, all pages share the same session object, and is user specific. 会话-在同一会话内的请求之间共享值,所有页面共享同一会话对象,并且是用户特定的。 ViewState - shares values between request but is page specific and user specific. ViewState-在请求之间共享值,但特定于页面和特定于用户。

if you want the benefit of static across a single request. 如果您希望通过单个请求获得静态收益。 That is, have a single request have access to the same value. 也就是说,只有一个请求可以访问相同的值。 Use a session variable instead. 请改用会话变量。

Session['variable'] = value

this will store the value in the current session, this way you will have a different value per request. 这会将值存储在当前会话中,这样您的每个请求的值将有所不同。

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

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