简体   繁体   English

ASP.NET/WCF/IIS中的静态变量的范围是什么?

[英]What is the scope of a static variable in ASP.NET / WCF / IIS?

There are two questions on here which both have highly-upvoted, but seemingly contradictory answers. 这里有两个问题都得到了很高的支持,但看似矛盾的答案。

What is the actual scope of a static variable? 静态变量的实际范围是什么?

In my case let's say I have a WCF service running under IIS. 就我而言,假设我有一个在IIS下运行的WCF服务。 Several servers with a load balancer in front. 多台服务器,前面有一个负载均衡器。 One Site on each server, one App Pool as well. 每台服务器上有一个站点,也有一个应用程序池。 Let's say there's a static variable stored in the class which implements the service. 假设在实现服务的类中存储了一个静态变量。

Will the variable persist across the worker process only? 变量将仅在工作进程中持久存在吗? The app pool? 应用程序池? The server? 服务器? I tried to research it but found two competing answers on here. 我试图对其进行研究,但是在这里找到了两个相互竞争的答案。

Under this post: IIS app pools, worker processes, app domains 在此帖子下: IIS应用程序池,辅助进程,应用程序域

The reply says "Each worker process is a different program that's run your site, have their alone [own?] static variables" 答复说:​​“每个工作进程都是运行您的站点的不同程序,它们都有单独的[own?]静态变量”。

Yet under this post: Lifetime of ASP.NET Static Variable 然而在这篇文章下: ASP.NET静态变量的生命周期

The reply says "The static variables are per pool" 答复说“静态变量是每个池”

Maybe I just don't understand the posts, but they seem contradictory? 也许我只是不理解这些帖子,但是它们似乎矛盾?

It appears I have several worker processes running when I checked. 当我检查时,似乎有几个工作进程正在运行。 Hence my question. 因此,我的问题。

Any help would be appreciated. 任何帮助,将不胜感激。 I am trying to refactor some stuff away from using static variables since it seems risky and exposes concurrency problems but I am very uncomfortable proposing changes without understanding the current behaviour. 我试图重构一些东西,使其不使用静态变量,因为这看起来很冒险并且暴露出并发问题,但是我在不了解当前行为的情况下提出更改感到非常不自在。 Thanks! 谢谢!

Static variables persist for the life of the application domain. 静态变量在应用程序域的生命周期中一直存在。 So there are 2 things that will reset your static variables is an application domain restart or the use of a new class 因此,有两件事会重置您的静态变量:重新启动应用程序域或使用新类

Each application pool can have multiple worker processes, 每个应用程序池可以有多个工作进程,

Each worker process will run different application instances. 每个工作进程将运行不同的应用程序实例。

Each instance of an application has a separate AppDomain - one per application instance. 一个应用程序的每个实例都有一个单独的AppDomain- 每个应用程序实例一个。

Static variables are lost when IIS restarts your asp.net application when any of the below happens 如果发生以下任何情况,当IIS重新启动asp.net应用程序时,静态变量将丢失

  1. The pool decide that need to make a recompile. 池决定需要重新编译。
  2. You open the app_offline.htm file 您打开app_offline.htm文件
  3. manual restart of the app pool 手动重启应用程序池
  4. The pool has reached some limits that is defined on the server and restart. 池已达到服务器上定义的某些限制,然后重新启动。
  5. Restart iis 重新启动iis

Static variables are not thread safe, and you need to use the lock keyword if you access them from different threads. 静态变量不是线程安全的,如果从不同的线程访问它们,则需要使用lock关键字。

Since an app restart will reset your statics variables, and you want to persist data across your application life time, you should store the data persistently in a DB or a file. 由于应用程序重启将重置您的静态变量,并且您希望在应用程序生命周期内保留数据,因此应将数据永久存储在数据库或文件中。 You can store information per-user in Session State with a database session state mode. 您可以使用数据库会话状态模式将每个用户的信息存储在会话状态中。

ASP.NET Application State/Variables will not help as they are stored in memory, hence they are not persistent, hence they are lost on app domain restart too. ASP.NET应用程序状态/变量将无济于事,因为它们存储在内存中,因此它们不是持久性的,因此在应用程序域重启时也会丢失。

当我们在Web中定义静态字段时,它将仅与应用程序域限制共享。

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

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