Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Restart and Stop of ASP.NET Application
ASP.NET web application could restart or stop for numerous reasons. Most of the time, we would like that ASP.NET website is alive.Web hosting companies often claim 99.99% up time. But, in reality it is hard to achieve 99.99% uptime. Even if server is online and working, that doesn't mean that ASP.NET web application is also active and working properly.
To save resources, web server could restart or completely stop web application many times per day. Every time when this happens, website visitors could experience slow page loads, sessions could be lost, or even see error message in browser like "408 Request Timeout", "500 Internal Server Error", "503 Service Unavailable" etc.
What happens when ASP.NET Application restarts
ASP.NET application restart will cause these consequences:
- Cache is cleared
To keep sessions when application restarts, use Session State Server or SQL Server. Notice that state server stores sessions in memory, so it is faster than SQL Server which stores data to database. But, for the same reason, SQL Server is more reliable option.
Possible reasons why ASP.NET application could restart
ASP.NET application could restart because really a lot of reasons. It probably happens more often than you think. ASP.NET web application will restart because of:
- Change in Web.Config file. Change of any parameter, adding of space character or even changing file's modified date will cause app restart.
How to change website configuration without restarting ASP.NET application
Any change in Web.config file will cause ASP.NET application to restart. But, it is possible to create one or more external .config files which don't cause application restart. To connect external .config files with main Web.config, use configSource parameter. Here is an example code snippet, used to read external configuration from three files:
So, in this example, we can change connection strings, application settings and mail settings. Application will not restart when any of these files change. Any external .config file should contain only section where is called. For example, db.config is called from <connectionStrings /> and should contain only connection strings, like this:
How to restart ASP.NET Application with code
To restart ASP.NET web application programmatically with server side code, use
method. This method will stop and unload web application. Application_End procedure in Global.asax will be executed. Application will start again on next request.
How to stop or get offline ASP.NET web application
The reason why you want to stop ASP.NET application could be if you do big website upgrade, which could include updating of pages, database, dlls etc. This process can take some time. If visitor tries to see pages, could experience errors. Sounds better to place a message that site is down for maintenance and that will be live again at certain time.
One more reason could be to unlock resources for updating. Some resources are locked while application is running, like SQL Server Express .mdf file in App_Data folder or MS Access .mdb file. You can't in the same time upload new database file and have active connections.
There are three common ways to make ASP.NET application offline:
- Using IIS to stop website
How to make website offline using IIS
Simple solution is to stop website from IIS management console. More visitor friendly is to redirect all requests to info page where you can explain why website is down and when will be active again. On IIS 6,
On IIS7, Http redirection is not available by default. First, go to Control Panel -> Turn Windows Features On or Off -> Internet Information Services -> World Wide Web Services -> Common HTTP Features -> and select Http Redirection like in next image:
Now, when you start IIS and select website, you will see new "HTTP Redirect" icon in Features View. Use is almost the same like on IIS 6, you need to specify exact destination for all requests. In addition to that, IIS 7 gives an option to set status code that will be returned to browser.
Accessing to Control Panel and IIS is impossible if you are on shared hosting where developer usually has very limited rights. Fortunately, ASP.NET offers similar option using App_Offline.htm file.
Going offline with App_Offline.htm file
Very simple way to make your web application offline is by using App_Offline.htm file. Create new HTML page and name it "App_Offline.htm". Copy the page to root folder of website. Now, if you try to visit website in browser, server will return only App_Offline.htm. Web server will shut down the application and unload it
On this page, you can place some information to visitors about why website is down, and when they could expect to be online again. You can put practically anything in this file, but be careful about its size. If App_Offline.htm is smaller than 512 bytes Internet Explorer (occurst on IE 6) will show its own HTTP error message. To avoid this, be sure that App_Offline.htm has more than 512 bytes, even if you place some invisible HTML comments.
When website upgrading is finished, delete or rename App_Offline.htm from root folder. Web application will go online again after next request.
Keep in mind that solution with App_Offline.htm page works only for ASP.NET pages. Many web hosting providers today offer several technologies in same hosting package, so you can use ASP.NET, PHP, classic ASP etc., all mixed on single website. Notice that any PHP, ASP or static HTML pages will still work normally after App_Offline.htm is uploaded.
Make website offline using httpRuntime element in web.config
To make web application offline, you can use Web Site Administration Tool. If you can't use WSAT, edit web.config manually. Add <httpRuntime enable="false" /> inside <system.web> tag, like this:
Application will not be loaded to memory. Any request to ASP.NET page will return error 404 "The resource cannot be found". This error is not friendly as App_Offline.htm file where you can inform visitors what is happening.
<httpRuntime enable="false"/> affects only ASP.NET pages, so content, like images, static HTML pages, classic ASP pages etc., will still be available.
Website is automatically stopped when IIS recycled process
After some time of inactivity (20 minutes by default) IIS will unload ASP.NET application. Application will load again on first request, but without cache, application state etc., like when application is restarted.
Application start could take few seconds, so first response is usually much slower. It could be annoying if you do your best to optimize code, database, images and everything else to get faster website and then wait long seconds in front of screen until application is started.
If you have an access to IIS, you can create new or change existing application pool. Uncheck Recycling Conditions and increase Idle Time-out (in minutes).
Also, it is possible to keep web application alive by making periodical requests. The fastest option is to use some web hosting automation program, like DotNetPanel, Plesk, Helm etc. If your hosting provider enables scheduled tasks option, you can create new task in Helm to call certain web page, and set execution time every 20 minutes. Now, because page is requested every 20 minutes, Idle Time-out will never occur and website will have fast response any time. There are some more options to implement scheduled tasks, described in Using Task Scheduler or Windows Service For Scheduled Tasks tutorial.
As you see, there are many reasons that cause ASP.NET application to stop or restart. You probably can't avoid application restart sometime. Nor you should, this is useful process when web server free memory. Just be sure that restart doesn't occur to often, since that could hurt visitor's experience. Users' sessions could be saved if you use Session State Server or SQL Server, instead of default InProc option.
There are few options to get website offline. The most reliable is using of IIS because it will redirect all requests. But, if you have not access to IIS management console, you have App_Offline.htm and <httpRuntime enable="false" /> options. Just be aware that last two affects only ASP.NET files.
If there are no visitors on website, IIS will unload application. When first visitor comes, application will be loaded again. This sounds good in theory, but in practice this visitor will wait long time until requested page is shown. To keep low traffic website alive, make periodical artificial requests using windows service or some other scheduled tasks option. More about scheduled tasks read in Using Task Scheduler or Windows Service For Scheduled Tasks and Scheduled Tasks in ASP.NET tutorials.