的HttpModule初始化方法被调用了几次 - 为什么? [英] HttpModule Init method is called several times - why?

查看:214
本文介绍了的HttpModule初始化方法被调用了几次 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个HTTP模块在调试时我发现了一些起初(至少)似乎是怪异的行为。

当我在HTTP模块的init方法设置断点,我可以看到,HTTP模块init方法被调用了几次,尽管我只启动了网站进行调试,并提出一个单一的请求(有时被击中只有1次,其他时间多达10次)。

我知道,我应该想到的HttpApplication的几个实例在运行,并为每一个HTTP模块将被创建,但是当我请求一个页面,应该由一个单一的HTTP应用程序对象进行处理,因此只能触发事件相关的一次,但仍激发的事件多次为每个请求这是没有意义的 - 除了它肯定是这样的HttpApplication内增加数倍 - 这意味着它是一种被称为每次而不是相同的HttpModule初始化方法新的HTTP应用程序被每次打我的破发点时创建(见底部等。我的code为例)。

什么地方出错了怎么回事?是不是因为我调试和设置断点的HTTP模块?

这已经注意到,它看来,如果我启动的网站进行调试和快速跨过了HTTP模块只会打init方法一次,同样适用于事件处理程序的断点。如果我不是让它在几秒钟init方法被调用多次(好像这取决于我跨过断点之前的时间等待多长时间)断点挂起。也许这可能是一些建立在功能,以确保该HTTP模块被初始化和HTTP应用程序可以请求提供服务,但它也似乎喜欢的事,可能会产生灾难性的后果。

这可能似乎是合乎逻辑的,因为它可能会尝试完成请求,因为我已经设置了断点它自以为事情已经错了,并尝试再次调用init方法?洙它可以处理请求?

但是,这是发生什么事,一切正常(我只是猜测),或者是一个实际的问题?

我所特别关心的是,如果事情使得它挂在生产/生活服务器几秒大量的事件处理程序是通过init添加的,因此每个请求的页面突然触发的事件处理程序的几个次。

此行​​为可能会很快带来任何网站了。

我已经看过了原生态。NET $ C $用于的HttpModules的formsauthentication和rolemanagermodule等C,但我的code心不是任何不同之处在于这些模块使用。

我的code看起来是这样的。

 公共无效的Ini​​t(HttpApplication的应用程序)
{
如果(CommunityAuthenticationIntegration.IsEnabled)
{
FormsAuthenticationModule formsAuthModule =(FormsAuthenticationModule)app.Modules [FormsAuthentication];

formsAuthModule.Authenticate + =新FormsAuthenticationEventHandler(this.OnAuthenticate);
}
}
 

下面是一个例子它是如何从.NET框架RoleManagerModule完成

 公共无效的Ini​​t(HttpApplication的应用程序)
{
如果(Roles.Enabled)
{
app.PostAuthenticateRequest + =新的EventHandler(this.OnEnter);
app.EndRequest + =新的EventHandler(this.OnLeave);
}
}
 

做任何人都知道是怎么回事?

(我只是希望有人在那里能告诉我为什么发生这种情况,并告诉我,一切都完美的罚款):)


更新:

我试图缩小问题,到目前为止,我已经发现,Init方法被调用总是在我的HTTP模块的新对象(contary什么,我以前的想法)。

我看来,对于第一个请求(启动时通过网站)中的所有的HttpApplication对象被创建,它的模块都试图成为第一个请求,因此全部命中将被添加的事件处理程序。 我真的不能弄清楚为什么会这样。

如果我要求另一页所有的HttpApplication的创建(和他们的moduless)将再次尝试服务请求,把它打造成该事件处理程序多次。

但是,这也似乎是,如果我再跳回第一页(或另一个)只有一个的HttpApplication将开始采取要求照顾,一切都如预期 - 只要我不让它在休息挂点。

如果我让它在断点处挂起它开始创建新的HttpApplication的对象,并开始增加HttpApplications(大于1)服务/处理请求(这已经是在所服务的这是目前停在HttpApplication的过程断点)。

我想还是希望它可能是一些聪明的幕后的帮助分发和处理负载和/或错误的方式。但我不知道。 我希望有在那里能告诉我,这是完全正常的,它是如何应该是什么?

解决方案
  1. 检查HttpContext.Current.Request看,有什么要求模块的初始化被激发。可能是浏览器发送多个请求。

  2. 如果您连接到IIS,做检查IIS日志,了解任何请求是否被接收为你是住在破发点的时间。

I was creating a http module and while debugging I noticed something which at first (at least) seemed like weird behaviour.

When i set a breakpoint in the init method of the httpmodule i can see that the http module init method is being called several times even though i have only started up the website for debugging and made one single request (sometimes it is hit only 1 time, other times as many as 10 times).

I know that I should expect several instances of the HttpApplication to be running and for each the http modules will be created, but when i request a single page it should be handled by a single http application object and therefore only fire the events associated once, but still it fires the events several times for each request which makes no sense - other than it must have been added several times within that httpApplication - which means it is the same httpmodule init method which is being called every time and not a new http application being created each time it hits my break point (see my code example at the bottom etc.).

What could be going wrong here ? is it because i am debugging and set a breakpoint in the http module?

It have noticed that it seems that if i startup the website for debugging and quickly step over the breakpoint in the httpmodule it will only hit the init method once and the same goes for the eventhandler. If I instead let it hang at the breakpoint for a few seconds the init method is being called several times (seems like it depends on how long time i wait before stepping over the breakpoint). Maybe this could be some build in feature to make sure that the httpmodule is initialised and the http application can serve requests , but it also seems like something that could have catastrophic consequences.

This could seem logical, as it might be trying to finish the request and since i have set the break point it thinks something have gone wrong and try to call the init method again ? soo it can handle the request ?

But is this what is happening and is everything fine (i am just guessing), or is it a real problem ?

What i am specially concerned about is that if something makes it hang on the "production/live" server for a few seconds a lot of event handlers are added through the init and therefore each request to the page suddenly fires the eventhandler several times.

This behaviour could quickly bring any site down.

I have looked at the "original" .net code used for the httpmodules for formsauthentication and the rolemanagermodule etc but my code isnt any different that those modules uses.

My code looks like this.

    public void Init(HttpApplication app)
	{
		if (CommunityAuthenticationIntegration.IsEnabled)
		{
			FormsAuthenticationModule formsAuthModule = (FormsAuthenticationModule) app.Modules["FormsAuthentication"];			

			formsAuthModule.Authenticate += new FormsAuthenticationEventHandler(this.OnAuthenticate);
		}
	}

here is an example how it is done in the RoleManagerModule from the .NET framework

    public void Init(HttpApplication app)
	{
		if (Roles.Enabled)
		{
			app.PostAuthenticateRequest += new EventHandler(this.OnEnter);
			app.EndRequest += new EventHandler(this.OnLeave);
		}
	}

Do anyone know what is going on?

(i just hope someone out there can tell me why this is happening and assure me that everything is perfectly fine) :)


UPDATE:

I have tried to narrow down the problem and so far i have found that the Init method being called is always on a new object of my http module (contary to what i thought before).

I seems that for the first request (when starting up the site) all of the HttpApplication objects being created and its modules are all trying to serve the first request and therefore all hit the eventhandler that is being added. I cant really figure out why this is happening.

If i request another page all the HttpApplication's created (and their moduless) will again try to serve the request causing it to hit the eventhandler multiple times.

But it also seems that if i then jump back to the first page (or another one) only one HttpApplication will start to take care of the request and everything is as expected - as long as i dont let it hang at a break point.

If i let it hang at a breakpoint it begins to create new HttpApplication's objects and starts adding HttpApplications (more than 1) to serve/handle the request (which is already in process of being served by the HttpApplication which is currently stopped at the breakpoint).

I guess or hope that it might be some intelligent "behind the scenes" way of helping to distribute and handle load and / or errors. But I have no clue. I hope some out there can assure me that it is perfectly fine and how it is supposed to be?

解决方案

  1. Inspect the HttpContext.Current.Request to see, for what request the module's init is fired. Could be browser sending multiple request.

  2. If you are connected to IIS, do check IIS logs to know whether any request is received for the time you are staying at the break point.

这篇关于的HttpModule初始化方法被调用了几次 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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