A quick touch for restarting ASP.NET applications

By: John Kaster

Abstract: Quick and easy restarting of ASP.NET applications

    So many web applications, so little time

Sometimes when you have lots of web applications and lots of web servers (like we do on the developer network), you want to restart or reload a set of applications or a set of servers. Often, the server gets rebooted to do this, but many times that is far too "heavy" for simply restarting a web application, particularly when it's ASP.NET. Rebooting the server makes the service unavailable far longer than simply restarting the web application.

Whenever the web.config file for an ASP.NET application is updated, the ASP.NET application will restart. This is usually gracefully handled for browsers using that application, because the applications are primarily stateless, and the viewstate feature of ASP.NET supports graceful resumption of operations for these web application. (Lots of information on viewstate can be found with this Google search.)

    A nice touch

With ASP.NET, you often end up with multiple web.config files in a bunch of different directories, and manually updating or touching them all is usually quite painful, which encourages people to simply reboot the server. The venerable Borland touch utility can come to your rescue in this case. This utility ships with Delphi, C#Builder, and C++Builder. When you execute touch with no parameters, this is what it displays:

Touch Version 4.2 Copyright (c) 1998 Borland International

Usage: TOUCH [options] [filename(s)...]     (wild cards * and ? ok)
  -c           don't create file
  -D           allow directory names to be touched (when wildcards specified)
  -h           prints this help screen (-h2 for expanded help)
  -rfilename   set to same date/time as 'filename'
  -s           recurse through subdirectories
  -thh:mm[:ss] set the file time
  -dmm/dd/yy   set the file date

    Gentlemen, (re)start your applications!

As you can see from the output above, you can recurse through your subdirectories for wildcards to touch. This means a command line like:

touch -c -s d:\webapps\web.config

Will cause every ASP.NET application found underneath the d:\webapps directory to reload itself. The –c command line switch prevents the utility from creating a web.config file where it shouldn't exist, and the –s switch tells it to search every subdirectory of d:\webapps to find all web.config files.

More importantly, touch also supports UNC file names, so you can recursively touch files on a server, provided your active user account has sufficient rights for doing it:

touch -c -s \\<machine name>\<share location for asp.net apps> \web.config

This means I can restart all ASP.NET applications on any of the BDN servers, or create a batch file to restart all web applications on multiple servers, with simple command-lines like:

touch -c -s \\webserver\d\apps\web.config

This call results in output similar to the following:

Touch  Version 4.2  Copyright (c) 1998 Borland International

Unable to locate: \\webserver\d\apps\bdn\article\web.config
Unable to locate: \\webserver\d\apps\bdn\bin\web.config
Unable to locate: \\webserver\d\apps\bdn\images\web.config

For every folder containing a web.config file, its timestamp will be updated. For folders that do not have a web.config file, touch says it can't find it. This way, you can verify for yourself that it is correctly traveling through all subfolders.

I always enjoy finding new uses for old utilities, and the quick and convenient solution provided by touch is one of the better ones I've rediscovered lately. I hope you find it useful as well.

Server Response from: ETNASC04