C# ASP .NET 单例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP .NET Singleton
提问by Wesly
Just want to make sure I am not assuming something foolish here, when implementing the singleton pattern in an ASP .Net web application the static variable scope is only for the current user session, right? If a second user is accessing the site it is a different memory scope...?
只是想确保我没有在这里假设一些愚蠢的事情,在 ASP .Net Web 应用程序中实现单例模式时,静态变量范围仅适用于当前用户会话,对吗?如果第二个用户正在访问该站点,则它是不同的内存范围......?
采纳答案by Otávio Décio
The static variable scope is for the entire app domain, which means other sessions also have access to it. Only if you have a farm with different servers you would have more than one instance of the variable.
静态变量范围适用于整个应用程序域,这意味着其他会话也可以访问它。仅当您拥有一个包含不同服务器的场时,您才会拥有多个变量实例。
回答by NotMe
If you need it to be user or session based then check out the following link. Otherwise, as Otavio said, the singleton is available to the entire domain.
如果您需要它是基于用户或会话的,请查看以下链接。否则,正如 Otavio 所说,单例可用于整个域。
回答by Tomas Vana
The singleton is used for the entire Application Domain, if you want to store user session-related data, use HttpContext Session which is designed for that purpose. Of course, you probably have to redesign your class structure to be able to come up with a key-value-pair way of dealing with the data you're trying to work with.
单例用于整个应用程序域,如果要存储与用户会话相关的数据,请使用为此目的设计的 HttpContext Session。当然,您可能必须重新设计您的类结构才能提出一种键值对方法来处理您尝试使用的数据。
回答by Dan Herbert
As others have mentioned, a static variable is global to the entire application, not single requests.
正如其他人所提到的,静态变量对整个应用程序是全局的,而不是单个请求。
To make a singleton global to only individual requests, you can use the HttpContext.Current.Items
dictionary.
要将单例全局设置为仅针对单个请求,您可以使用HttpContext.Current.Items
字典。
public class Singleton
{
private Singleton() { }
public static Singleton Instance
{
get
{
if (HttpContext.Current.Items["yourKey"] == null)
HttpContext.Current.Items["yourKey"] = new Singleton();
return (Singleton)HttpContext.Current.Items["yourKey"];
}
}
}
回答by Moshe Bixenshpaner
Static members have a scope of the current worker process only, so it has nothing to do with users, because other requests aren't necessarily handled by the same worker process.
静态成员只有当前工作进程的作用域,所以它与用户无关,因为其他请求不一定由同一个工作进程处理。
- In order to share data with a specific user and across requests, use HttpContext.Current.Session.
- In order to share data within a specific request, use HttpContext.Current.Items.
- In order to share data across the entire application, either write a mechanism for that, or configure IIS to work with a single process and write a singleton / use Application.
- 为了与特定用户和跨请求共享数据,请使用 HttpContext.Current.Session。
- 为了在特定请求中共享数据,请使用 HttpContext.Current.Items。
- 为了在整个应用程序中共享数据,要么为此编写一种机制,要么将 IIS 配置为与单个进程一起工作并编写一个单例/使用应用程序。
By the way, the default number of worker processes is 1, so this is why the web is full of people thinking that static members have a scope of the entire application.
顺便说一句,工作进程的默认数量是 1,所以这就是为什么网络上到处都是认为静态成员具有整个应用程序范围的人的原因。
回答by Johnny W
Session for entire application per user. ViewState for single asp page.
每个用户的整个应用程序的会话。单个asp页面的ViewState。