Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is Application_End called in asp.net WebService

When exactly does the Application_End fire in the case of a WebService ??

I read (Application_End global.asax) that the Application_End is called everytime the application is unloaded. Does this mean after every call to a method in a web service ?

I have a piece of code that I need fired only once on the first call to the IIS, and again after the last call to the IIS (and between recycles), and I can't have it being fired upon every WebService request and response...

like image 209
gillyb Avatar asked Dec 28 '10 07:12

gillyb


People also ask

When application_ End is called?

This means that an old app pool's End might be called after the new app pool's Start . But this should not matter, because each app has it's own AppDomain, and doesn't share data. (but sometimes that can explain otherwise weird behavior.) Oh, and finally; even that is configurable, too! EDIT: One more thing to add!

What is Application_End?

Application_End: Fired when the last instance of an HttpApplication class is destroyed. It is fired only once during an application's lifetime. Application_BeginRequest: Fired when an application request is received. It is the first event fired for a request, which is often a page request (URL) that a user enters.

When application_ End event is fired in ASP net?

The Application_End event gets fired when the IIS pool is recycled or when you make changes to the bin folder or web. config file. You should change the default IIS setting to schedule a recycle once a day on offpeak hours.

What is application_ Start?

The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values. You should set only static data during application start.


1 Answers

Application_End is exactly what you are looking for; The application is unloaded according to the configuration you set, but by default it will continue running for a certain amount of time of being idle after any requests come in, or it will remain running while requests are continually coming in.

Note that other things can cause the App Pool to refresh, and therefore cause Application_End to be called; a certain number of recompiles (due to changed aspx files, etc), a certain time period running, a certain amount of memory pressure, etc. Again, these are all configurable, but are set to reasonable defaults, generally.

The key thing to keep in mind is that you can expect there to be some time between Application_Start and Application_End, but you can't know how much time there will be, based on what is happening on the server.

Also note that when an App Pool is recycled, already-running requests are not stopped suddenly, and they may in fact overlap with new requests being handled by the new process. This means that an old app pool's End might be called after the new app pool's Start. But this should not matter, because each app has it's own AppDomain, and doesn't share data. (but sometimes that can explain otherwise weird behavior.) Oh, and finally; even that is configurable, too!


EDIT: One more thing to add! Note that if the server is taken down suddenly, Application_End would not be called.

like image 89
Andrew Barber Avatar answered Oct 17 '22 10:10

Andrew Barber