简体   繁体   English

Asp.NET MVC应用程序中的静态类

[英]static class in Asp.NET MVC app

I am wondering if a static class in an ASP.NET MVC app could be initialized more than once . 我想知道ASP.NET MVC应用程序中的静态类是否可以多次初始化 I initially designed my app so that a static component would fetch some stuff from the database and serve as a cache, and I added a refresh method to the class which was called from the constructor. 我最初设计我的应用程序,以便静态组件从数据库中获取一些东西并充当缓存,我向从构造函数调用的类添加了一个刷新方法。 The refresh method was also made available through the administration portion of the app. 刷新方法也通过应用程序的管理部分提供。 At some point I noticed that the data was updated without requiring this manual trigger, which implies that the static constructor run more than once. 在某些时候,我注意到数据已更新,无需手动触发,这意味着静态构造函数不止一次运行。

There are several scenarios where I could reasonably see this happen, such as an unhandled Exception causing re-initialization. 有几种情况我可以合理地看到这种情况发生,例如未处理的Exception导致重新初始化。 But I am having trouble reproducing this, so I would like to know for sure. 但我很难复制这个,所以我想肯定。

The most usual scenarios would be: 最常见的情况是:

  • a reload of the web application 重新加载Web应用程序

    • touched Web.config 触摸了Web.config
    • touched binaries 感动二进制文件
    • abnormal termination (out-of-memory, permission errors) 异常终止(内存不足,权限错误)
  • a reload of the Application Pool 重新加载应用程序池

  • a restart of IIS 重启IIS
  • a restart of w3wp.exe (at least once in 29 hours!) 重启w3wp.exe(至少29小时一次!)

The app-domain will get reloaded (recompiling the dynamic parts as necessary) and this would void any statically initialized data. app-domain将被重新加载(根据需要重新编译动态部分),这将使任何静态初始化的数据无效。

You can get around that by persisting the static data somewhere if creating it is expensive, or avoid reloading the AppDomain, the Application Pool or the IIS server. 如果创建静态数据很昂贵,或者避免重新加载AppDomain,应用程序池或IIS服务器,可以通过在某处保留静态数据来解决这个问题。

Update : Phil Haack just published a relevant blog entry here: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx 更新 :Phil Haack刚刚在这里发布了相关博客文章: http//haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

  • Bye Bye App Domain Bye Bye App Domain
    • it does a better job at explaining the above. 它在解释上述方面做得更好。 Notable, IIS will recycle it's worker process very 29 hours at minimum and shared hosters will recycle AppDomain much more often (perhaps in 20 minutes of idle time) 值得注意的是,IIS将至少29小时回收它的工作进程,并且共享的托管服务商将更频繁地回收AppDomain(可能在20分钟的空闲时间内)
  • So tell ASP.NET, “Hey, I'm working here!” 告诉ASP.NET,“嘿,我在这里工作!”
    • outlines techniques you can apply to get notified of an AppDomain take down - you could use this to get your Singleton instance behaving 'correctly' 概述了可以应用以获得AppDomain取消通知的技术 - 可以使用此技术让您的Singleton实例正常运行
  • Recommendation 建议

I suggest you read it :) 我建议你看看:)

static classes are initialized once per AppDomain. 每个AppDomain初始化一次static类。

If IIS recycles your AppDomain, everything will re-initialize. 如果IIS回收您的AppDomain,一切都将重新初始化。

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

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