示例#1
0
        static void OnFileChanged(object sender, FileSystemEventArgs args)
        {
            if (HttpRuntime.DomainUnloading)
            {
                return;
            }
            string name     = args.Name;
            bool   isConfig = false;

            if (StrUtils.EndsWith(name, "onfig", true))
            {
                if (String.Compare(Path.GetFileName(name), "web.config", true, Helpers.InvariantCulture) != 0)
                {
                    return;
                }
                isConfig = true;
            }
            else if (StrUtils.EndsWith(name, "lobal.asax", true) && String.Compare(name, "global.asax", true, Helpers.InvariantCulture) != 0)
            {
                return;
            }

            Console.WriteLine("Change: " + name);

            // {Inotify,FAM}Watcher will notify about events for a directory regardless
            // of the filter pattern. This might be a bug in the watchers code, but
            // since I couldn't find any rationale for the code in there I'd opted for
            // not removing it and instead working around the issue here. Fix for bug
            // #495011
            FileSystemWatcher watcher = sender as FileSystemWatcher;

            if (watcher != null && String.Compare(watcher.Filter, "?eb.?onfig", true, Helpers.InvariantCulture) == 0 && Directory.Exists(name))
            {
                return;
            }

            // We re-enable suppression here since WebConfigurationManager will disable
            // it after save is done. WebConfigurationManager is called twice by
            // Configuration - just after opening the target file and just after closing
            // it. For that reason we will receive two change notifications and if we
            // disabled suppression here, it would reload the application on the second
            // change notification.
            if (isConfig && WebConfigurationManager.SuppressAppReload(true))
            {
                return;
            }

            lock (watchers_lock) {
                if (app_shutdown)
                {
                    return;
                }
                app_shutdown = true;

                // Disable event raising to avoid concurrent restarts
                DisableWatchers();

                // Restart application
                HttpRuntime.UnloadAppDomain();
            }
        }