在Asp.NET MVC应用程序的静态类 [英] static class in Asp.NET MVC app

查看:87
本文介绍了在Asp.NET MVC应用程序的静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果一个静态类 ASP.NET MVC 的应用可能会不止一次初始化多。我最初的设计我的应用程序,使静态的组件会从数据库中获取一些东西,并作为缓存,我添加了一个刷新方法这是从构造函数调用的类。刷新方法还通过应用程序的管理部分提供。在某些时候,我注意到,数据,而不需要这种手动触发,这意味着静态构造函数运行多次更新。

有几种情况,我可以合理地看到这样的情况,比如未处理例外导致重新初始化。但我无法重现这一点,所以我想知道。


解决方案

最常用的场景是:


  • Web应用程序的重新加载


    • 触及的Web.config

    • 感动二进制文件

    • 异常终止(外的内存,允许错误)


  • 应用程序池的重新加载


  • IIS重新启动

  • w3wp.exe的重新启动(至少有一次29个小时!)

该应用程序域将得到重​​新加载(如需要重新编译动态部分),这都将使任何静态初始化数据。

您可以通过持续的静态数据,如果某个地方建立它是昂贵的,或避免重装的AppDomain中,应用程序池或IIS服务器绕开这个问题。


  

更新的:菲尔哈克刚刚出版了相关的博客条目在这里:<一href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\" rel=\"nofollow\">http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx


  
  

    

        
  • 再见应用程序域
        

          
    • 它在解释上述做得更好。值得注意的,IIS将回收它的工作进程非常最小29小时和共享托管会更经常回收的AppDomain(也许是20分钟的空闲时间)

    •     

  •     
  • 那么告诉ASP.NET,嘿,我在这里工作!
        

          
    • 概述技术,您可以申请获得通知一个AppDomain中取下来 - 可以使用它来得到你的Singleton实例行为'正确'

    •     

  •     
  • 建议

  •     

  

我建议你读它:)

I am wondering if a static class in an ASP.NET MVC app could be initialized more than once. 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. 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

    • touched Web.config
    • touched binaries
    • abnormal termination (out-of-memory, permission errors)
  • a reload of the Application Pool

  • a restart of IIS
  • a restart of w3wp.exe (at least once in 29 hours!)

The app-domain will get reloaded (recompiling the dynamic parts as necessary) and this would void any statically initialized data.

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.

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

  • 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)
  • So tell ASP.NET, "Hey, I’m working here!"
    • outlines techniques you can apply to get notified of an AppDomain take down - you could use this to get your Singleton instance behaving 'correctly'
  • Recommendation

I suggest you read it :)

这篇关于在Asp.NET MVC应用程序的静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆