何时在Global.asax中使用的Application_Start VS初始化? [英] When to use Application_Start vs Init in Global.asax?

查看:163
本文介绍了何时在Global.asax中使用的Application_Start VS初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在什么情况下我应该把应用程序初始化code在的Application_Start() VS 的init()在我的的Global.asax 文件?

I am wondering under what circumstances I should be putting application initialisation code in Application_Start() vs Init() in my Global.asax file?

这两者之间的区别似乎并没有对我比的Application_Start 很明显,对方先被调用,那么的init()

The distinction between the two doesn't seem very obvious to me, other than Application_start gets called first, then Init().


  • 为什么要使用一个比其他?

  • 是否真正有所作为?

  • 什么应用程序状态发生改变
    这两个事件之间?

到目前为止,唯一真正的指针我能找到的是, IHttpModule的只能有一个的init()方法,所以如果什么我在某些时候做可能更适合实施 IHttpModule的我应该使用的init()方法的的Global.asax ,如果不出意外的一致性。

So far the only real pointer I can find is that IHttpModule only has an Init() method, so if what I'm doing may at some point be better suited to implement IHttpModule I should use the Init() method of Global.asax, if nothing else for consistency.

推荐答案

从的 MSDN文档

在的Application_Start和Application_End方法是特殊的方法不重新present的HttpApplication事件。 ASP.NET的称他们一旦应用程序域的生命周期,而不是​​每一个HttpApplication实例

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.

初​​始化:

名为一次的每个实例
  的HttpApplication
类毕竟
  模块已被创建。

Called once for every instance of the HttpApplication class after all modules have been created.

更新:如果你需要确保一定的code被称为只有在应用程序一次。生命周期的Application_Start是一个更好的解决方案。示例:配置log4net的

UPDATE: if you need to make sure a certain code is called only once in the app. lifecycle, Application_Start is a better solution. Examples: configuring log4net?

这篇关于何时在Global.asax中使用的Application_Start VS初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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