绝对最早的地方执行顺序来运行ASP.NET code [英] Order of execution of the absolute earliest places to run code in ASP.NET

查看:188
本文介绍了绝对最早的地方执行顺序来运行ASP.NET code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多地方,你可以有初始化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无法确定。埃里克利珀最近发布了一个伟大的 4 - part 博客 详细说明他们是如何工作的系列。

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.

至于你提到的其余项目,标有<一个方法href=\"http://msdn.microsoft.com/en-us/library/system.web.$p$papplicationstartmethodattribute.aspx\"><$c$c>System.Web.$p$pApplicationStartMethodAttribute首先执行。根据MSDN文档此属性,也没有在这些方法调用的顺序的保证。

As for the rest of the items you mentioned, methods marked with the System.Web.PreApplicationStartMethodAttribute are executed first. According to the MSDN documentation for this attribute, there is no guarantee of the order in which these methods are called.

根据博客文章按菲尔哈克,此属性使开发人员有机会调用应用程序的启动过程中另外两个方法:<一href=\"http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.registerbuildprovider.aspx\"><$c$c>BuildProvider.RegisterBuildProvider和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.addreferencedassembly.aspx\"><$c$c>BuildManager.AddReferencedAssembly.为 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 之前动态注册<一个标记的所有方法href=\"https://github.com/davidebbo/WebActivator/blob/master/WebActivator/ActivationManager.cs#L131\">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:


  1. Web.config文件读入内存

  2. 方法打上 preApplicationStartMethodAttribute

  3. HttpApplication.Application_Start

  4. 标记方法 WebActivator.PostApplicationStartMethodAttribute

  1. Web.config is read into memory
  2. Methods marked with a PreApplicationStartMethodAttribute
  3. HttpApplication.Application_Start
  4. Methods marked with WebActivator.PostApplicationStartMethodAttribute

这篇关于绝对最早的地方执行顺序来运行ASP.NET code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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