(IIS7) How to configure the w3wp process to never go down - iis-7

I have one web app installed on one of my web servers.
Its quick response is a must for user experience (i.e. returns phrases to the autocomplete)
After this application spops receiving requests for a while, w3wp process goes down and on the next request, the response time will be longer, as it will take also the time to load the w3wp process.
Is there a way to configure the w3wp process to never go down?
Is my only solution is to write an exe that sends dummy request every 1 minute?
Is there a built in "Keep alive" mechanism?
Thanks.

In the Application Pool settings, change the value used for Idle-Timeout within the Process section. I believe the default is 20 minutes.

Related

ASP.NET Core 3.1 application does not terminate process after Program.Main finishes running

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

What method is called right before the application exits?

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.

Ensure dot.net site always runs

I've created a very small monitor web-app, which periodically polls other websites to check if they're alive. If they're not I'm sent an e-mail so I can react (e.g. if the site responds 503)
However my problem is that the monitor web-app is shutdown when the AppPool recycles, and it's never started again because it gets no requests.
Is there a way to ensure it'll start again after a recycle, restart of the server or similar ?
Currently it starts a background thread in global.asax Application_Start, and the thread is then in charge of keeping the monitor web-app alive, by polling it as well as the other sites.
This way it'll get a HTTP request every so often. However this obviously only works for keeping it alive, when it has actually been started.
I've read a bit about IIS Warm Up modules, however the site is hosted on a server I've no influence on, so that's not possible.
The site is built using ASP.Net 4.0 and it runs on an IIS 7.5 server.
Hope you have an idea :-)
I use uptime robot to ensure my application is always spun up. The beauty of this system is it only asks for the headers of a page on your website and gets status codes like "200-ok", "404-not found", etc. every 5 minutes. This means that uptime robot does not add noise to Google Analytics as the page is never requested.
Seems to work like a dream for me and its FREE for upto 50 sites!
You should implement this as a windows service or console app run via a scheduled task. A web site is really not the appropriate type of application.
I Agree with tvanfosson but if you need it right now, you can still configure the application pool not to recycle.
http://technet.microsoft.com/en-us/library/cc753179(WS.10).aspx
It's better to make this up as a windows service or a scheduled console application. If you want to keep it as a webapplication then you can have 3rd party send pings to your application to keep it running. At my current client this is done with http://www.pingdom.com/ but there are other alternatives to it.

Why does IIS7 take a long time

It looks likes if I don't visit my low traffic site for a day, it takes a long time for the first page to load. I believe it's probably because IIS7 shuts down the application when it receives no requests for a certain length of time.
How can I stop this from happening?
I have a dedicated server so I have all the access required to change things in IIS
There are two ways that you can handle this.
Modify the "Idle Timeout" value within the application pool. By default it will shutdown the application if there are no requests for 20 minutes
If you are using ASP.NET 4.0 you can use the new Auto-Start behavior to keep the app "Always Running" you can see this blog post for examples on how to configure it.
The app pool goes to sleep basically because it has no new requests to process in a certain amount of time.
There is a plug-in for iis that can fix this:
IIS: Application Initialization Module for IIS 7.5
Works great for both new deployments and idle applications.
Take a look at the application pool, check the advanced settings->process model->idle timeout (minutes). Set this higher than 20 mins. Sounds like the worker process is shutting down because its idle.
http://technet.microsoft.com/en-us/library/cc771956(WS.10).aspx
Cheers
tigger

ASP.NET MVC: First access after some minutes slow, then every following request is fast

when I access any page of my ASP.NET MVC website first time, then this first request is slow. It needs about 4-5 seconds to load. But every following request to any page is fast.
When I wait some minutes or a hour then every first request is slow again. Every following request is fast.
I think that IIS 7 is compiling the code and keep it in memory. After some time it will delete it from memory so it needs to compile it again.
What can I do that every first request is as fast as every following request?
(Without precompiling my source, if possible)
Thank you very much in advance!
If this is a production server then why not try adding a website monitor; such as up time robot. It basically asks for your websites headers and gets status codes like "200-ok", "404-not found", etc. every 5 minutes. This way your site is always spun up and does not impact log files/analytics as only headers are requested. I use this for my cloud sites as I find that they take 5 seconds to spin up which has an impact on site loading. With the monitor they are instant.
Oh and its free for up to 50 sites!
This could be the recycle worker processes setting on the application pool, check the value for that and either turn it off or make it longer.
Could also be the Shutdown worker process after being idle under performance for the application pool.
It's probably the second as that defaults to 20 minutes, the first one defaults to 29 hours I believe.
This is almost-certainly your app pool idle timeout setting (and not your code being recompiled).
The default app pool idle timeout in IIS is 20 minutes. Which means that, if 20 minutes pass and no requests come in to your app, IIS will shut down the worker process for your app pool, making your app "cold" again. Whoever makes the next request will be waiting several seconds as IIS restarts the worker process and "warms" your app back up.
If you don't want IIS to automatically "cool down" your app after a period of inactivity, you can disable the app pool idle timeout by setting it to 0.
Assuming that you have a regular stream of visitors, this should not be a problem in production. Also, unless you're physically altering any of your source files, IIS would not recompile it when it spins up your app.
Also, take a look at the .NET compilation settings available:
http://technet.microsoft.com/en-us/library/cc725812(WS.10).aspx
tl; dr
After quite extensive testing and gathering of relevant sources to attempt to resolve the issue, I think that the minimal solution may be (not verified) to add this code to the global.asax:
protected void Application_End()
{
...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://web_domain.tld");
using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse())
{
}
...
}
In the time of writing the post, this is used to effectevely maintain the site I worked on 'always-up' - with conjuction of setting the Idle Time-out (minutes) in IIS to 0 as mentioned in other answers. However I think that changing Idle Time-out (minutes) may not be necessary (assuming that Application_end event is raised when the switch of application pool to Idle mode occurs).
How it works (assuming it does):
There are 2 settings in the IIS application pool settings that affect when the application is suspended or terminated. One of them is Idle Time-out (minutes) which defaults to 20 minutes and when the specified time elapses since the last request, the worker process is suspended or terminated. When a next request comes, the worker process is resumed or started again and the Application_start event is raised (and so if the Application_start handler is defined in global.asax, it is executed). In case of the project I worked on, the Application_start took about 17 seconds to complete. So if the site was 'left alone' for 21 minutes and then the new request came, it took about a little more than 17 seconds before the response was sent back (Application_start + page processing). When another request was sent in the window of 20 minutes, the response was sent significantly faster (perhaps less than 1s), since the Application_start has already been processed.
Setting the Idle Time-out (minutes) value to 0 causes the worker process to never be suspended / terminated (at least due to the idle time - there may be another cause described below).
Apart from Idle Time-out (minutes), there is also the Regular time interval (minutes) setting in Recycling section of the IIS advanced settings. This defaults to 1740 minutes (29 hours) and is a regular scheduled task that causes the Application pool to periodically recycle (the period is fixed). This, I understood, is to prevent the accumulation of possible memory-leaks which, if present, may eventually crash the server by exhasting all the memory. The effect of app-pool recycling is that Application_end event is raised. However, after the application ends it is not automatically started again, so the Application_start event is not raised until an actual request comes (which is similar to the first case). So in the case of the aforementioned web application I worked on, it would again take about 17 seconds to process the first request to arrive after the recycling occured. Changing this value to 0 turns the recycling off, but I suppose that it is reasonable not to turn the recycling completely off due to the possibility of accumuluation of memory leaks over time (which may possibly even be caused by bugs in 3rd party libraries). I understand that the opinions differ on whether to set this value to 0 or not and may change over time.
Posible solution (without changing IIS settings):
If the requests are sent frequently enough, the app-pool may never switch to Idle mode. On the event of recycling, this would also cause to the application to start again. This can be achieved, for example, by using 3rd party service as described by #Rippo.
Proposed solution:
It has been observed that application pool recycling caused Application_end event to be raised. Assuming that it is also raised upon switching of the application pool to Idle mode, it would seem to be sufficient to create a request in the Application_end event handler to the website, which in both cases would then cause the application to start again (raising the Application_start event). The code is at the top of the answer.

Resources