views:

27

answers:

2

I have a webservice where I want to do something when a the application pool ends, so I thought I'd do:

Application_End()
{
    // Some logic here
}

What happens is if I stop the application pool, this logic is executed. On the other hand, if I just call iisreset, it is NOT.

So my question is: where should I put my code so that it is executed in both cases?

A: 

I don't think you could. Imagine if your code fired off an infinite loop, you could basically kill the web server and stop it shutting down.

Ben Robinson
Following that logic, doing the same in Application_End() would prevent an application pool from stopping, which would have similar impact. I won't say you're wrong. It just doesn't sound right.
Francisco Silva
+1  A: 

There is no guarantee that Application_End will be called. The example you mentioned, where you perform an IIS reset is an example. Other examples could include someone unplugging the server, or a hardware failure.

What I've done in the past is to use Application_Start to call my data cleanup logic as the application comes back online. This is assuming you don't need any values that were stored in memory.

AaronS
Well, in order to do my cleanup I need the references I have in memory. I guess I will have to serialize them somehow so I can do the cleanup.
Francisco Silva