简体   繁体   中英

Shut Down Web Application

I need to shut down my web application during maintenance process, have already gone through many ways like putting app_offline.htm in root directory , disabling the Runtime or disabling it manually via server but i what i need to implement is to do this process completely automated.

What i have is the start and end dates for shut down and flag for those days ie whether application needed to be shut down on those dates.

Solution that i already have is to create a job in sql server agent which creates and deletes the app_offline.htm file in and from the root directory but what the problem is i need to give static path for the root directory of my application which i don not want to do.

You can use the appcmd command line utility for managing sites on IIS. It's located in %systemroot%\\system32\\inetsrv\\APPCMD . I think it is available in IIS v7 and above only though, not sure if your using an older version of IIS.

To stop and start a site, the command will look like the following:

%systemroot%\system32\inetsrv\APPCMD stop site <Your Site's Name>

%systemroot%\system32\inetsrv\APPCMD start site <Your Site's Name>

More info on the appcmd utility is here: http://www.iis.net/learn/get-started/getting-started-with-iis/getting-started-with-appcmdexe

This is part of the answer which deals with probing the IIS to get the physical path. It might need some tweaking

         //eg of site = 'Default Web Site'
        //eg of Application = 'MySite'
        var appName = "MySite";
        //Initializes a new instance of the ServerManager class by using the default path of the ApplicationHost.config file.
        ServerManager mgr = new ServerManager();
        var applications = mgr.Sites.Cast<Site>().SelectMany(s => s.Applications);

        var app = applications.SingleOrDefault(a => a.Path.Equals("/" + appName));

        IList<string> physicalPaths = app.VirtualDirectories.Cast<VirtualDirectory>().Select(v => v.PhysicalPath).ToList();

        //Calling dispose manually. Per MSDN, cannot wrap the ServerManager instance in 'Using' as it causes memory leaks
        mgr.Dispose();

        //Releasing the reference to the Server Manager, per MSDN guidance
        mgr = null;
        return physicalPaths;

One issue that you have here is that web application work on a request basis. You make a request, request is processed and returned. Therefore, to rely on this principle to shutdown your application will not work. What you need is to register a scheduler of some type in Application_Start that would configure itself based on the values in the database. Although I am not sure which scheduling mechanism would be appropriate, you might want to look at Timer (but you must keep a reference to the this object because of garbage collection) or Task scheduler in System.Threading.Tasks namespace .

I might be wrong with a choice of classes but this could be a starting point.

Now, as for you design decision, I would avoid it completely. If your web application can create app_offline.html or rename a file into that one, you have no way of bringing the server back online without manual intervention by removing the file. Instead of that why not create some maintenance Windows Service that can query the database and take offline and bring back online again? If you don't care about bringing the web application online automatically then you should not care about taking it offline automatically.

Another thing to consider is a human mistake in configuring the time when application goes offline. Wrongly configured time can bring down your application much too sooner or much later. Wouldn't it be easier if you created some batch scripts or PowerShell scripts that could take down and bring back up the web application? With the PowerShell script you can query IIS for your application without specifying any physical location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM