简体   繁体   中英

How to detect when a page is being update to IIS

Whenever I update my web app on IIS, any user who currently using it, will see the page be unresponsive and it won't work again until they refresh the browser. (The update process last for about 30 seconds)

I would like to show up a notification, such as a javascript alert, for user to know that the page is being udpated and please try to refresh the page after 30 seconds, etc.

I tried to catch the Exception in Global.ascx but no exception was thrown in this case.

You could create a FileSystemWatcher in global.ascx then bubble up (update a js for instance) an exception when a file is updated. You could start with this:

using System.IO;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        FileSystemWatcher watcher;

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            watcher = new FileSystemWatcher(this.Context.Server.MapPath("/"));
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            //set a value in js file
            FileInfo jsFilesChanged = new FileInfo(Path.Combine(this.Context.Server.MapPath("/"), "scripts", "files_changed.js"));
            using (StreamWriter jsWriter = (!jsFilesChanged.Exists) ? new StreamWriter(jsFilesChanged.Create()) : new StreamWriter(jsFilesChanged.FullName, false))
            {
                jsWriter.WriteLine("var changed_file = \"" + e.Name + "\";");
            }
        }

        //.......
    }
}

Then in client code include files_changed.js and create a periodic timeout call to check the var changed_file . Also, make sure watcher doesn't get garbage collected.

Some references: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

http://www.developerfusion.com/article/84362/extending-filesystemwatcher-to-aspnet/

Consider using app_offline.htm . It is a page that will cause clients to see your IIS app as being down. When you're through updating, just remove the page.

How the big boys do this:

You need to have a way of posting an alert on a page. Typically this is done by having a table in your database for these alerts. Basically you are just storing some text in there like "hey, the site is going down for maintenance between 8:00am and 8:01am"..

On each page load, you check that table and display any messages found in a conspicuous place (like the top).

Prior to pushing an update you add the alert, while giving them enough time to wrap up whatever it is that they are doing.

After the push is complete you clear out the alerts table.

Honestly the main issue you have is simply one of scheduling updates and communicating to the users what's about to happen. You want to do so in a way that isn't a surprise. That said, you might consider enabling the optimizeCompilations flag in order to try and speed up the compilation time of your website when it is first hit after pushing an update.

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