简体   繁体   English

跨不同ASP.NET请求的静态属性

[英]Static properties across different ASP.NET requests

If I create a static property MyLanguage and one request sets its value to 1 , while at the same time another thread sets it to 2 - what will be the final value of MyLanguage ? 如果我创建一个静态属性MyLanguage并且一个请求将其值设置为1 ,同时另一个线程将其设置为2 - MyLanguage的最终值是MyLanguage

Does the single MyLanguge property gets shared across ASP.NET sessions? 是否在ASP.NET会话中共享单个MyLanguge属性?

A static property/field is shared across the app domain. 应用程序域之间共享静态属性/字段。 So all your sessions should see the same value . 因此,您的所有会话都应该看到相同的值

The only exception is when you use the ThreadStatic attribute on a static field , in which case each thread will see its own value. 唯一的例外是当您在静态字段上使用ThreadStatic属性时,在这种情况下,每个线程都将看到自己的值。 eg 例如

[ThreadStatic]
static int counter = 0; // each thread sees a different static counter.

It would be 2. Static fields, properties are shared between objects.So Latest set values will be updates for all the instances. 它将是2.静态字段,属性在对象之间共享。因此,最新的设置值将是所有实例的更新。

From MSDN 来自MSDN

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object . 使用static修饰符声明一个静态成员,该成员属于该类型本身而不是特定对象 The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes. static修饰符可以与类,字段,方法,属性,运算符,事件和构造函数一起使用,但不能与索引器,析构函数或类以外的类型一起使用。 For more information 欲获得更多信息

Statics are unique to the application domain, all users of that application domain will share the same value for each static property. 静态对于应用程序域是唯一的,该应用程序域的所有用户将为每个静态属性共享相同的值。

When you see the word static, think "there will only be one instance of this." 当你看到单词static时,请想“只会有一个这样的实例”。 How long that instance lasts is a separate question, but the short answer is that it is variable. 该实例持续多长时间是一个单独的问题,但简短的回答是它是可变的。

If you want to store values specific to the user look into Session State . 如果要存储特定于用户的值,请查看会话状态

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

相关问题 ASP.NET Web Api:跨请求公开一个大型的静态集合 - ASP.NET Web Api: Exposing a large, static collection across requests ASP.NET中作为静态字段和属性的全局变量是否不需要锁定? - Global variables in ASP.NET as static fields and properties no locking required? 静态属性如何在asp.net环境中工作? - How do static properties work in an asp.net environment? 静态属性/集合不能在ASP.NET应用程序中正确执行 - Static Properties/Collections Not Executing Properly Inside ASP.NET Application 跨不同程序集的ASP.NET可重用用户控件 - ASP.NET Reusable User Controls Across Different Assemblies 模拟来自不同位置的HTTP请求 - ASP.Net - Simulate HTTP requests from different location - ASP.Net 在ASP.NET MVC应用程序的实例之间共享具有非静态成员的静态类? - Static class with non-static members shared across instances of ASP.NET MVC app? Azure上ASP.NET中Web套接字静态列表的不同实例 - Different instances of the static list of web sockets in ASP.NET on Azure asp.net core基于角色的不同角色的授权作用 - asp.net coreRole-based Authorization different properties of the role 跨多个请求在无状态环境中保持文件锁定(Asp.Net Core) - Maintaining a lock on files in a stateless environment across multiple requests (Asp.Net Core)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM