1

In my Global.asax.cs file I have the following methods:

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("<!-- Empty Message! -->");
    }
    else {
        HttpContext.Current.Response.Write("<!-- Message: " + message + " -->");
    }
}

When I run locally, the message is output after the closing </html> tag.

When I deploy to the test server the message is not output, not even the <!-- Empty Message! --> one. I can even add code to throw an exception in the Application_EndRequest method which never gets thrown.

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.

I discovered this as part of an investigation as to why HttpContext.Current.Items was losing values during a request, again only when deployed to the test server. (Similar to this unresolved problem)

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 web.config. In fact the only change was this to get around the empty page problem

<system.webServer>
    <modules>
        <remove name="UrlRoutingModule-4.0" />
        <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
</system.webServer>

Does anyone know why Application_EndRequest would not be getting called on the server?

Is there yet another configuration setting needed?

Update 201412191534

I finally got Glimpse working. I added some TraceInformation messages to Application_Start, Application_BeginRequest and Application_EndRequest. 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 HttpContext.Current.Items - they had not been set in the first place!

Update 201412221010

I have now looked into why the events in Global.asax are not getting called and found many similar questions:

I have tried such suggestions as: - making sure the Global.asax inherits from the correct class (It does);
- setting a code break point (which is never reached because the events aren't fired)

Then I found this one - Global.asax not firing for .aspx pages in IIS7 which suggested using the <modules runAllManagedModulesForAllRequests="true"> which I hadn't because I was using the approach of adding the following to the modules section

<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />

because everyone recommends not using runAllManagedModulesForAllRequests="true". See this article Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work

However, in a desparate attempt I used this evil attribute and guess what? The events started getting fired again!

We haven't applied the hotfix and I haven't found out how to add modules into the modules section i.e. the correct syntax for my application.

Community
  • 1
  • 1
Mark_Gibson
  • 922
  • 1
  • 12
  • 32
  • This is not a duplicate of the linked question. My problem is I'm setting something in Application_BeginRequest, NOT Application_Start, and the corresponding Application_EndRequest does not get called – Mark_Gibson Dec 19 '14 at 09:02
  • Why do you need to do this in global.asax at all when you can do a global action filter that would add something to your response? – Vsevolod Goloviznin Dec 19 '14 at 09:32
  • @VsevolodGoloviznin this is just a simple example to show that EndRequest is not getting called. – Mark_Gibson Dec 19 '14 at 09:45
  • Just a suggestion...but have you tried restarting the Application Pool for your server? Just because your App Domain which ASP.NET runs under restarts doesnt mean the app pool loads up another HttpApplication instance. Try that each time you deploy when you have no visitors to the site and see if that loads up your event changes. – Stokely Jun 29 '17 at 11:04

1 Answers1

0

The HttpApplication instance that is represented by your global.asax file is a single instance that only represents the first HttpApplication object. It is not guaranteed that this instance of the HttpApplication will be used for any other request.

You need to override the Init() method in global.asax and in that method hook up any events that you want:

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}

Please refer to this MSDN article for more info on the HttpApplication object.

This answer was found here.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • If the question is the same, and the answer is the same, it would be more appropriate to vote to close as a duplicate than to copy the answer here. – mason Dec 18 '14 at 16:45
  • I'd be interested to know why this situation has never occurred when using MVC3. Also, why it never occurs when running locally in debug mode. It is a well used technique for ensuring one dbContext per request i.e. setting it in BeginRequest and disposing it in EndRequest as described [here](http://stackoverflow.com/questions/6334592/one-dbcontext-per-request-in-asp-net-mvc-without-ioc-container) – Mark_Gibson Dec 19 '14 at 08:34
  • Now that I've read the MSDN article and the linked question, this does not answer the question because I am setting something in BeginRequest but the corresponding EndRequest does not get called – Mark_Gibson Dec 19 '14 at 09:00