My app uses data stored in cache, no more than 30MB at any instant. This data is important, so I would prefer not to loose it on unwanted crashes or recycles. Is there any method or way to make a backup of this data in SQL Server when the app recycles? Is there any method which is called when the app recycles where I can make a backup on SQL Server when that happens?
Add code to save your data to file or database in global.asax in Application_End(). In a polite shutdown that should get called.
There are some instances when Application_End event will not be fired when there is not a polite shutdown. These are covered here and summarized below:
This event fires when a live application is gracefully shutdown. The
following things can cause this event to fire:
You edit the config file for an application that's running. This causes ASP.NET to unload the app and reload it, so Application_End
will fire
You change a dll in the bin directory. This also causes ASP.NET to unload the app and reload it, so Application_End will fire
You stop (or restart) IIS
If you have any sort of Process Recycling turned on either in IIS6 App Pools, or using the aspnet worker process, then when that process
recycles the Application_End event will fire for all active
applications
Any other similar event that causes processes to gracefully restart.
It does NOT fire when a process doesn't gracefully exit - like it's
deadlocked and needs to be killed. Also It does NOT fire when all live
sessions have ended, although all active sessions will end before the
application ends in the above scenarios. The subtle difference is that
all live sessions can end but the app is still running idle.
Related
I have a rather large ASP.NET Core application running in an IIS application pool with overlapped recycling. I have set IHostApplicationLifetime handlers and I see that it is hitting all of the lifecycle methods as well as the AppDomain.CurrentDomain.ProcessExit handler that I've set. When I request a recycle, here's what happens:
ApplicationStopping is hit
ApplicationStopped is hit
Dependencies configured in Startup are disposed
Breakpoint after webHost.Run() in Program.cs is hit (this is the last line of the Main method of the program)
ProcessExit handler is run
At this point I would expect w3wp to be dead. But no, it lingers around for quite a while after, and continues to take a lot of memory. I also can view it in Process Explorer and I see it still has a bit of activity.
I guess the obvious things to look for are things that need to be disposed, but I don't know how to look for these. How can I figure out why w3wp will not go away for such a long time / what is keeping it around?
As far as I know, hit the ProcessExit handler means the asp.net core application has been down, not the w3wp.exe.
The w3wp.exe is managed by IIS not the websites. The IIS will check the w3wp.exe is free or not(handing request). If there is no request coming in about 20 minutes(by default). Then the IIS will stop the w3wp.exe.
The IIS console manager contains the idle timeout value. If you want to shut down the w3wp.exe as soon as possible. I suggest you could reduce the value to 1 which means if there is no request coming in 1 minutes, it will stop the w3wp.exe.
More details about how to set it, you could refer to below steps:
Open the IIS management console and find the application pool
2.Click the advanced setting and modify idle time-out
We are dealing with a problem of an application restarting too often (once every couple of hours). This results in the first visitor having a long load time every few hours. Furthermore, we have a separate server that handles queued messages, it may be down for a day without us knowing because the application will exit for no apparent reason.
Is there any event or method we could hook into to log when the application exits, why it exits, and possibly instruct it to restart?
IIS will shut down any app pool that is idle for a period of time. IIS will also recycle the app pool when it detects certain kinds of errors, etc...
You can certainly hook into the Application_End() event, however it is not guaranteed that this will be called in all cases. For instance, if the app is forcefully terminated it will not get called.
In more recent versions of IIS, you can configure IIS to keep your app "warm". You can also create a scheduled job that just touches your homepage every so often.
I believe what you're looking for is application_end, which gets fired when your IIS pool is recycled, or the application gets unloaded. Applications reload themselves when a required file changes as well.
I would like to fire a method 'ProcessBatch () ' to run every 'x' minutes when an ASP.Net app starts up.
I think I will have to add code for this to Application_Start event.
What is the best way of doing this, so that when the website has its app pool recycled then the scheduled task that is running is treated as a web request and allowed to complete?
You cannot create a scheduled task that runs "every x minutes" on a web server.
Some people write services that run on a client computer which pings the site every now and then. But for that you need that client to be up-and-running.
One workaround is, like you say, the Application_Start event, but your application may run longer than just a few minutes. We do not actually know when that is.
You could run your task someplace else, like the Session_Start event (whenever a new user opens one of your pages), or even the BeginRequest event if you write your own HttpModule.
In those events, you could remember the time the task was run for the last time (like a static, or in the ApplicationState maybe) to keep it from running hundreds of times per minute once your site has a huge load of users.
Hope this helps?
Also, depending on where your site is hosted, you may be able to use that provider's scheduled-task system. Azure has one, RackspaceCloud and DiscountASP.NET as well.
If I:
put an app_offline.html in the website root
stop an IIS 7.5 site
stop an IIS 7.5 App Pool
What happens to currently running threads (in all three cases) including background threads? Are they all immediately terminated, or are running ones allowed to finish?
Thanks
Andrew
By adding the app_offline.htm the application send the Application_End and after this function return the rest of the threads of the program are killed. The maximum time of wait for the Application_End to return is set on the pool settings.
If you stop the full pool then all the sites that under this pool follow the same procedure. If you only open the app_offline.htm then only this site affected.
To avoid your threads to kill by this shutdown, set a wait state on the Application_End
void Application_End(object sender, EventArgs e)
{
// This is a custom function that you must make and
// check your threads in the program
MyTheadClass.WaitForAllMyThreadsToExist();
// after this function exit the rest of the threads are killed.
}
One more note.
All the time that you wait on Application_End for your threads to exit from the wait state (of a mutex probably) the site is not longer accept web connections and it seems like not responding. So its up to you to make your threads exit as soon as possible when you get this message.
Just noting that the file name must be app_offline.htm. Using app_offline.html will not work!
When app_offline.html is put:
Based on ScottGu's this article it says the app domain is unloaded. And based on this article, all background threads are stopped when the app domain is unloaded and ThreadAbortException is thrown.
I'm not sure about stopping the site but stopping the App Pool should have the same effect.
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...
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.