为什么HttpApplication的构造函数调用多次 [英] Why is HttpApplication constructor called several times

查看:208
本文介绍了为什么HttpApplication的构造函数调用多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么从派生的自定义类的构造函数的的HttpApplication 被称为在应用程序启动几次?

Can somebody explain why the constructor of a custom class derived from HttpApplication is called several times upon application startup?

我的code结构如下:

  - 我的全球在Global.asax中类从 CustomApp 类派生

  - 在 CustomApp 类派生自的HttpApplication

My code structure is the following:
- My Global class in global.asax derives from CustomApp class.
- The CustomApp class derives from HttpApplication class

全球在启动时创建的类,但是当我把一个断点在构造函数,调用它几次!我认为应该只有一个实例的应用类产生的?

The Global class is created at startup, but when I place a breakpoint in the constructor, it is invoked several times! I thought there should be only one instance of Application class created?

我错了?

UPD :Web服务器确实可以创建多个的HttpApplication 实例来处理在同一时间进来的多个请求。当你把一个断点在的HttpApplication 后裔的构造函数,这就变得尤为明显。多个请求会从客户端(HTTP内容,CSS文件等)和服务于每个人的Web服务器将创建HttpApp的新实例来悬而未决。因此,编写应用程序的初始化逻辑,当提防这一点。

UPD: the web server can indeed create several HttpApplication instances to process multiple requests coming in at the same time. This becomes especially apparent when you place a breakpoint in the constructor of your HttpApplication descendant. Several requests will be pending from the client (http content, CSS files, etc) and to serve each of them the web server will create new instances of HttpApp. So, beware of this, when writing the application initialization logic.

推荐答案

我相信ASP.NET运行时有可能根据应用领域创建多个HttpApplication的。所以HttpApplication.Init和构造函数可以被调用一次以上。

I believe the ASP.NET runtime may create more than one HttpApplication per application domain. So HttpApplication.Init and the Ctor may get called more than once.

如果你想有初始化code只运行一次,你应该使用Application_Start事件这只会每个应用程序调用一次。

If you want to have initialization code that only runs once, you should use the Application_Start event which will only be called once per app.

这篇关于为什么HttpApplication的构造函数调用多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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