title: HttpModule Init method is called several times - why?
body: <p>I was creating a http module and while debugging I noticed something which at first (at least) seemed like weird behaviour.</p>
  
  <p>When I set a breakpoint in the init method of the httpmodule I can see that the <em>http module init method</em> 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).</p>
  
  <p>I know that I should expect several instances of the <em>HttpApplication</em> 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 <em>httpApplication</em> - which means it is the same <em>httpmodule init method</em> 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.).</p>
  
  <p>What could be going wrong here? Is it because I am debugging and set a breakpoint in the http module?</p>
  
  <p>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 <em>init method</em> once and the same goes for the <em>eventhandler</em>. If I instead let it hang at the breakpoint for a few seconds the <em>init method</em> 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 initialized and the http application can serve requests , but it also seems like something that could have catastrophic consequences.</p>
  
  <p>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?</p>
  
  <p>But is this what is happening and is everything fine (I am just guessing), or is it a real problem?</p>
  
  <p>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 <em>init</em> and therefore each request to the page suddenly fires the <em>eventhandler</em> several times.</p>
  
  <p>This behaviour could quickly bring any site down.</p>
  
  <p>I have looked at the "original" .net code used for the httpmodules for <em>formsauthentication</em> and the <em>rolemanagermodule</em>, etc... But my code isn't any different that those modules uses.</p>
  
  <p>My code looks like this.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void Init(HttpApplication app)
  {
      if (CommunityAuthenticationIntegration.IsEnabled)
      {
          FormsAuthenticationModule formsAuthModule = (FormsAuthenticationModule) app.Modules["FormsAuthentication"];         
  
          formsAuthModule.Authenticate += new FormsAuthenticationEventHandler(this.OnAuthenticate);
      }
  }
  </code></pre>
  
  <p>Here is an example how it is done in the <em>RoleManagerModule</em> from the .NET framework:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void Init(HttpApplication app)
  {
      if (Roles.Enabled)
      {
          app.PostAuthenticateRequest += new EventHandler(this.OnEnter);
          app.EndRequest += new EventHandler(this.OnLeave);
      }
  }
  </code></pre>
  
  <p>Does anyone know what is going on?</p>
  
  <p>(I just hope someone out there can tell me why this is happening and assure me that everything is perfectly fine) :)</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I have tried to narrow down the problem and so far I have found that the <em>init</em> method being called is always on a new object of my http module (contrary to what I thought before).</p>
  
  <p>I seems that for the first request (when starting up the site) all of the <em>HttpApplication</em> objects being created and their modules are all trying to serve the first request and therefore all hit the <em>eventhandler</em> that is being added.
  I can't really figure out why this is happening.</p>
  
  <p>If I request another page all the <em>HttpApplication</em>'s created (and their modules) will again try to serve the request causing it to hit the <em>eventhandler</em> multiple times.</p>
  
  <p>But it also seems that if I then jump back to the first page (or another one) only one <em>HttpApplication</em> will start to take care of the request and everything is as expected - as long as I don't let it hang at a break point.</p>
  
  <p>If I let it hang at a breakpoint it begins to create new <em>HttpApplication</em>'s objects and starts adding <em>HttpApplications</em> (more than 1) to serve/handle the request (which is already in process of being served by the <em>HttpApplication</em> which is currently stopped at the breakpoint).</p>
  
  <p>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?</p>