问题描述
有许多地方,你可以有初始化code在ASP.NET中执行:
There are numerous places you can have initialization code be executed in ASP.NET:
- 的web.config处理
- WebActivator
preApplicationStartMethod
- WebActivator
PostApplicationStartMethod
- 的Global.asax
的Application_Start
- web.config is processed
- WebActivator
PreApplicationStartMethod
- WebActivator
PostApplicationStartMethod
- Global.asax
Application_Start
什么是这些OCCURENCES的排序?是否有应该是这个名单?
What is the ordering of these occurences? Are there any other additional items that should be to this list?
编辑:由于有人提到静相关的第一次调用的位置,我要打破这个为他们
Since it was mentioned that statics are relevant to first invocation location, I'm going to break this up for them
这是在WebActivator使用Foo类 preApplicationStartMethod
Foo class that is used in a WebActivator PreApplicationStartMethod
- 静态构造函数
- 静态只读字段
这是在WebActivator用于酒吧类 PostApplicationStartMethod
Bar class that is used in a WebActivator PostApplicationStartMethod
- 静态构造函数
- 静态只读字段
这是在Global.asax中使用的巴兹类的Application_Start
Baz class that is used in a Global.asax Application_Start
- 静态构造函数
- 静态只读字段
有关清楚起见,假设在上述实施例的每个的那些依赖于富/酒吧/巴兹类中的位置被使用,并且该类包含一个静态构造和静态只读字段。
For clarity purposes, suppose that in the above examples each of those depends on the Foo/Bar/Baz class being used in the location and that the class contains a static constructor and static readonly field.
推荐答案
静态构造函数和静态字段初始化由运行时,ASP.NET无法确定。埃里克利珀最近发布了一个伟大的 - 的 详细说明他们是如何工作的系列。
Static constructors and static field initialization is determined by the runtime, not ASP.NET. Eric Lippert recently posted a great four-part blog series detailing how they work.
至于你提到的其余项目,标有按菲尔哈克,此属性使开发人员有机会调用应用程序的启动过程中另外两个方法:<$c$c>BuildProvider.RegisterBuildProvider$c$c>和<$c$c>BuildManager.AddReferencedAssembly$c$c>.为 BuildManager.AddReferenceAssembly
MSDN文档指出,这种方法只能在应用程序的Application_ preStartInit阶段,这表明是被执行时标注的所有方法的System.Web。preApplicationStartMethodAttribute
执行。
According to a blog post by Phil Haack, this attribute gives developers the opportunity to call two other methods during the application's startup: BuildProvider.RegisterBuildProvider
and BuildManager.AddReferencedAssembly
. The MSDN documentation for BuildManager.AddReferenceAssembly
states that this method can only be executed during the Application_PreStartInit stage of the application, which suggests that's when all methods marked by the System.Web.PreApplicationStartMethodAttribute
are executed.
WebActivator使用框架的 preApplicationStartMethodAttribute
挂钩到应用程序的启动。一旦调用,它将搜索并执行由 WebActivator。preApplicationStartMethodAttribute
之前动态注册an HTTP模块稍后将通过调用所有标注方法 PostApplicationStartMethodAttribute
- 已的Application_Start在HttpApplication类被称为后
WebActivator uses the framework's PreApplicationStartMethodAttribute
to hook into the application's startup. Once called, it will search for and execute all methods marked by the WebActivator.PreApplicationStartMethodAttribute
before it dynamically registers an HttpModule that will later invoke all methods marked by the PostApplicationStartMethodAttribute
- after Application_Start has been called in the HttpApplication class.
所以,总结一下,顺序是:
So, to summarize, the order is:
- Web.config文件读入内存
- 方法打上
preApplicationStartMethodAttribute
- HttpApplication.Application_Start
- 标记方法
WebActivator.PostApplicationStartMethodAttribute
- Web.config is read into memory
- Methods marked with a
PreApplicationStartMethodAttribute
- HttpApplication.Application_Start
- Methods marked with
WebActivator.PostApplicationStartMethodAttribute
这篇关于绝对最早的地方执行顺序来运行ASP.NET code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!