title: Application_EndRequest not called when application deployed to server
body: <p>In my  Global.asax.cs file I have the following methods:</p>
  
  <pre><code>protected virtual void Application_BeginRequest() {
      HttpContext.Current.Items["message"] = "Hello World";
  }
  
  protected virtual void Application_EndRequest() {
      string message = (string)HttpContext.Current.Items["message"];
      if (String.IsNullOrEmpty(message)) {
          HttpContext.Current.Response.Write("&lt;!-- Empty Message! --&gt;");
      }
      else {
          HttpContext.Current.Response.Write("&lt;!-- Message: " + message + " --&gt;");
      }
  }
  </code></pre>
  
  <p>When I run locally, the message is output after the closing <code>&lt;/html&gt;</code> tag.</p>
  
  <p>When I deploy to the test server the message is not output, not even the <code>&lt;!-- Empty Message! --&gt;</code> one.  I can even add code to throw an exception in the <code>Application_EndRequest</code> method which never gets thrown.</p>
  
  <p>I have used this approach with previous projects, built with VS2010 and MVC 4, mainly for the one dbcontext per request solution, with no problems.</p>
  
  <p>I discovered this as part of an investigation as to why <code>HttpContext.Current.Items</code> was losing values during a request, again only when deployed to the test server. (Similar to <a href="https://stackoverflow.com/questions/15948262/asp-net-httpcontext-items-vanish-on-server">this unresolved problem</a>)</p>
  
  <p>The application is straight 'out-of-the-box' using Visual Studio 2013 and MVC 5 (i.e. a new MVC application and no custom code), so there have been very few changes to <code>web.config</code>.  In fact the only change was this to get around the empty page problem</p>
  
  <pre><code>&lt;system.webServer&gt;
      &lt;modules&gt;
          &lt;remove name="UrlRoutingModule-4.0" /&gt;
          &lt;add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /&gt;
      &lt;/modules&gt;
  &lt;/system.webServer&gt;
  </code></pre>
  
  <p>Does anyone know why <code>Application_EndRequest</code> would not be getting called on the server?</p>
  
  <p>Is there yet another configuration setting needed?</p>
  
  <p><strong>Update 201412191534</strong></p>
  
  <p>I finally got Glimpse working.  I added some <code>TraceInformation</code> messages to <code>Application_Start</code>, <code>Application_BeginRequest</code> and <code>Application_EndRequest</code>.  Running  locally the trace messages appear.  Deploy app to test server and none of the messages appear.  So it would seem none of the methods on global.asax are getting called on the server, which would explain my missing <code>HttpContext.Current.Items</code> - they had not been set in the first place!</p>
  
  <p><strong>Update 201412221010</strong></p>
  
  <p>I have now looked into why the events in Global.asax are not getting called and found many similar questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/648713/events-in-global-asax-are-not-firing">Events in Global.asax are not firing</a></li>
  <li><a href="https://stackoverflow.com/questions/15503057/global-asax-events-not-firing-in-iis-6">Global.asax Events not firing in IIS 6</a></li>
  <li><a href="https://stackoverflow.com/questions/12890068/global-asax-is-not-publishing-and-event-are-not-firing-in-global-asax">Global.asax is not publishing and event are not firing in Global.asax</a></li>
  <li><a href="https://stackoverflow.com/questions/1028187/why-are-the-global-asax-events-not-firing-in-my-asp-net-website">Why are the Global.asax events not firing in my ASP .NET website?</a></li>
  </ul>
  
  <p>I have tried such suggestions as:
   - making sure the Global.asax inherits from the correct class (It does);<br>
   - setting a code break point (which is never reached because the events aren't fired) </p>
  
  <p>Then I found this one - <a href="https://stackoverflow.com/questions/2942797/global-asax-not-firing-for-aspx-pages-in-iis7">Global.asax not firing for .aspx pages in IIS7</a> which suggested using the <code>&lt;modules runAllManagedModulesForAllRequests="true"&gt;</code> which I hadn't because I was using the approach of adding the following to the <code>modules</code> section</p>
  
  <pre><code>&lt;remove name="UrlRoutingModule-4.0" /&gt;
  &lt;add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /&gt;
  </code></pre>
  
  <p>because everyone recommends not using <code>runAllManagedModulesForAllRequests="true"</code>.  See this article <a href="http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html" rel="nofollow noreferrer">Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work</a></p>
  
  <p>However, in a desparate attempt I used this evil attribute and guess what?  The events started getting fired again!  </p>
  
  <p>We haven't applied the <a href="http://support.microsoft.com/kb/980368" rel="nofollow noreferrer">hotfix</a> and I haven't found out how to add modules into the <code>modules</code> section i.e. the correct syntax for my application.</p>