对比的Application_Start与OnInit的构造 [英] Application_Start versus OnInit versus constructor
问题描述
我已经发这个自从我开始年前编程传统的ASP 12(左右),我从来没有发现一个很好的解决方案,因为ASP和ASP.NET的架构一直是陋习沼泽,魔术共享单身,等我最大的问题是与<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx\"><$c$c>HttpApplication$c$c>与非事件的事件对象(的Application_Start
, Application_End
,等等)。
I've gone rounds with this ever since I started programming classic ASP 12 (or so) years ago and I've never found a great solution because the architecture of ASP and ASP.NET has always been a swamp of bad practices, magic shared singletons, etc. My biggest issue is with the HttpApplication
object with its non-event events (Application_Start
, Application_End
, etc.).
如果你想为HTTP应用程序的整个生命周期做的东西一次,的Application_Start
是明显的地方去做。对?不完全是。首先,这不是一个事件本身,这是一个神奇的命名约定,其次当,则此方法将每个由IIS创建的AppDomain调用一次。
If you want to do stuff once for the entire lifespan of an HTTP application, Application_Start
is the obvious place to do it. Right? Not exactly. Firstly, this is not an event per se, it's a magic naming convention that, when followed, causes the method to be called once per AppDomain created by IIS.
除了神奇的命名规则是一个可怕的做法,我开始认为这可能是存在没有这样的东西在<$ C一开始
事件原因$ C>的HttpApplication 对象。所以我尝试了确实存在的事件,如初始化
。那么,这是不是一个真正的事件要么,这是一个重写的方法,这是未来最好的事情。
Besides magic naming conventions being a horrible practice, I've started to think it might be a reason there exist no such thing as a Start
event on the HttpApplication
object. So I've experimented with events that do exist, such as Init
. Well, this isn't really an event either, it's an overridable method, which is the next best thing.
看来,的init()
方法被调用为的HttpApplication
对象的每个实例,这恰好一很多比每个AppDomain一次。这意味着,我干脆把我的启动逻辑中的的HttpApplication
对象的构造函数。
It seems that the Init()
method is called for every instantiation of an HttpApplication
object, which happens a lot more than once per AppDomain. This means that I might as well just put my startup logic inside the HttpApplication
object's constructor.
现在我的问题是,为什么我不应该把我的启动逻辑中的构造?为什么连的init()
存在,我需要关心的Application_Start
?如果我这样做,任何人都可以解释为什么在的HttpApplication
对象这个伪事件没有适当的事件或重写的方法?
Now my question is, why shouldn't I put my startup logic in the constructor? Why does even Init()
exist and do I need to care about Application_Start
? If I do, can anyone explain why there is no proper event or overridable method for this pseudo-event in the HttpApplication
object?
和任何人都可以向我解释为什么在一个典型的ASP.NET应用程序的8个实例我的的HttpApplication
创建(这会使构造和初始化
跑就像很多次,当然,这可以通过锁定得到缓解,并称为共享静态布尔初始化
)时,我的应用程序只有一个AppDomain中?
And can anyone explain to me why in a typical ASP.NET application, 8 instances of my HttpApplication
are created (which causes the constructor and Init
to run just as many times, of course; this can be mitigated with locking and a shared static boolean called initialized
) when my application only has a single AppDomain?
推荐答案
调用的Application_Start第一次创建的HttpApplication实例,但不能在后续实例似乎有点一个黑客攻击。或许微软不想解释静态构造函数的人谁不真正想知道的概念。
Calling Application_Start the first time an instance of HttpApplication is created, but not on subsequent instances seems a bit of a hack. Perhaps Microsoft didn't want to explain the concept of a static constructor to people who didn't really want to know.
Application_End(),但是,似乎是必要的,因为没有C#等效静态析/终结的。由于黑客去,这是没有那么糟糕。它只是闻起来有点滑稽。
Application_End(), however, seems to be a necessity, as there is no C# equivalent of a static destructor/finalizer. As hacks go, this isn't that bad. It just smells a little funny.
这篇关于对比的Application_Start与OnInit的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!