Application_End is not fired when app unloaded. Why?

G

Guest

I am using Application_End to send out a notification about application being
unloaded. I found that those notifications are not being sent because the app
seems to get unloaded without Application_End ever being called. We started
logging Application_Start and Application_End events and found that the
number is not even: Application_Start happen more often than Application_End.
My question is: What are the sutuations in which we should expect
Application_End never being fired when application/process unloaded?
 
C

Curt_C [MVP]

1) are you sure the Application is ended?
2) what/how are you logging in the _end?
 
G

Guest

1. Yes. The app has a thread that sends heartbeats to report it's alive. We
noticed that after heartbeats stop coming (application unloaded),
Application_End doesn't always come after that.

2. We log it to Event Viewer.

Thank you,
Vlad Hrybok.
 
J

Juan T. Llibre

Hi, Vlad.

Are you running on IIS5 or IIS6 ?

If you're running on IIS 5.x, the ASP.NET service runs
under the ASPNET account, and that account has to be
granted privileges in order to write to the Event log.

If you're running on IIS 6, the ASP.NET service runs
under the NETWORK SERVICE account, and that
account has to be granted privileges in order to write
to the Event log.

Does your logging work OK when the Application_End
event does occur ? Or does the logging never happen ?

I did a little test, running on IIS 6, to check out if
Application_OnEnd firs when the Application is
forced to end ( and restart ).

Here's something I wrote which works every time
Application_OnEnd fires ( note: *not* Application_End):

First, grant the appropiate account
(ASPNET or NETWORK SERVICE) write permission
on any directory you'd like a text file to be written to,
depending on whether you're running IIS5.x or IIS 6.

Then, test this global.asax, which only has a WriteFile
function in the Application_OnEnd event :

global.asax:
------------
<%@ Application Language="C#"%>
<script runat="server">

protected void Application_OnEnd(Object sender, EventArgs e)
{
WriteFile("Application Ended ");
}
void WriteFile(string strText)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(@"c:\test.txt",true);
string str;
str = strText + " " + DateTime.Now.ToString( );
writer.WriteLine(str);
writer.Close( );
}
</script>
---------------

Now, open any *.aspx file in the Application, and then open the
global.asax file in notepad and hit enter to move the text in it
one line down and again back up one line, and save the file.

Editing global.asax will force Application_OnEnd to fire.

You should wind up with a text file in whatever directory
you designated in global.asax, with text similar to this :

------------------------------------------------------------
Application Ended 1/11/2005 2:23:51 AM
------------------------------------------------------------

It should work the same for your Event Logging.

If it doesn't, since this does, you could setup a
FileSystemWatcher Console app to do the logging
for you when changes are made to the text file.

See
http://msdn.microsoft.com/library/d.../frlrfSystemIOFileSystemWatcherClassTopic.asp
for sample code to do that.

Just substitute your logging code for this line:
Console::WriteLine(S"File: {0} {1}", e->FullPath, __box(e->ChangeType));

Try it out, and let us know how you do.




Juan T. Llibre
ASP.NET MVP
===========
 
G

Guest

Juan,
It's IIS 6 on Windows 2003 Server, but logging to the event log is not an
issue - we log just fine. (BTW, to write to the event log you don't have to
grant NETWORK SERVICE any privileges - as long as event source alredy exists
you can write into it from under NETWORK SERVICE just fine, and we create the
event source during the app installation. But it's besides the point.) The
problem is that Application_End doesn't seem to be called sometimes. I need
to determine when it's not being called. When I'm debugging the app and do
standard things that restart the app, like touching web.config or recycling
the app pool - I see application_end called fine. But there is a scenario
where you can see Start events logged and End not. It happens only when
debugging, but I wonder if something like this can happen during normal app
lifecycle?
To reproduce:
1. Build an app that logs Application_Start and Application_End events.
2. Run it by selecting Debug | Start from the menu so that browser opens up.
Observe Application_Start event logged.
3. Close the browser so that debug session ends.
4. Do Build | Rebuild from the menu.
5. Run the app again by selecting Debug | Start from the menu. Observe
Application_Start being logged w/o Application_End being logged.

Note: If you run the same scenarion one more time with one change: run the
app without the debugger, you will see Application_End being logged. The
purpose of the demo was to show that there can be situations when
Application_End not being called. Our problem in production is most likely
something else. Any idea what it could be?

Regards,
Vlad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top