public void ChangesToNewlyCreatedWatchedFileDoCauseCallback()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                using (FileStream differentFile = File.Create("Different.Test"))
                {
                    differentFile.Close();
                }

                Thread.Sleep(150);

                ChangeFile("Different.Test");

                Thread.Sleep(250);
            }

            File.Delete("Different.Test");

            Assert.AreEqual(1, configurationChangeCounter);
        }
        private ConfigurationChangeFileWatcher CreateWatcherForConfigSource(String configSource)
        {
            ConfigurationChangeFileWatcher watcher = null;

            if (MainConfigurationFileSource.Equals(configSource))
            {
                watcher = new ConfigurationChangeFileWatcher(mainConfigurationFileName,
                                                             configSource);
            }
            else
            {
                watcher = new ConfigurationChangeFileWatcher(Path.Combine(mainConfigurationFilePath, configSource),
                                                             configSource);
            }
            watcher.ConfigurationChanged += OnConfigurationChanged;

            configSourceWatcherMapping.Add(configSource, watcher);

            if (refresh)
            {
                watcher.StartWatching();
            }

            return(watcher);
        }
        public void OverwritingExistingWatchedFileCausesOnlyOneCallback()
        {
            if (File.Exists("Different.Test"))
            {
                File.Delete("Different.Test");
            }
            using (FileStream differentFile = new FileStream("Different.Test", FileMode.CreateNew))
            {
                differentFile.Close();
            }

            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.SetPollDelayInMilliseconds(100);
                watcher.StartWatching();
                Thread.Sleep(100);

                ChangeFile("Different.Test");
                Thread.Sleep(250);
            }

            File.Delete("Different.Test");

            Assert.AreEqual(1, configurationChangeCounter);
        }
示例#4
0
        public void Setup()
        {
            SystemConfigurationSource.ResetImplementation(false);

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            DummySection rwSection;

            rwConfiguration.Sections.Remove(localSection);
            rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
            rwSection.Name  = localSection;
            rwSection.Value = 10;
            rwSection.SectionInformation.ConfigSource = localSectionSource;

            rwConfiguration.Sections.Remove(externalSection);
            rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection());
            rwSection.Name  = externalSection;
            rwSection.Value = 20;
            rwSection.SectionInformation.ConfigSource = externalSectionSource;

            rwConfiguration.Sections.Remove(localSection2);
            rwConfiguration.Sections.Add(localSection2, rwSection = new DummySection());
            rwSection.Name  = localSection2;
            rwSection.Value = 30;
            rwSection.SectionInformation.ConfigSource = localSectionSource;

            rwConfiguration.Save();

            ConfigurationManager.RefreshSection(localSection);
            ConfigurationManager.RefreshSection(localSection2);
            ConfigurationManager.RefreshSection(externalSection);

            ConfigurationChangeFileWatcher.ResetDefaultPollDelay();

            updatedSectionsTally = new Dictionary <string, int>(0);
        }
        public void ChangesToNewlyCreatedWatchedFileDoCauseCallback()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                using (FileStream differentFile = File.Create("Different.Test"))
                {
                    differentFile.Close();
                }

                Thread.Sleep(150);

                ChangeFile("Different.Test");

                Thread.Sleep(250);
            }

            File.Delete("Different.Test");

            Assert.AreEqual(1, configurationChangeCounter);
        }
        public void Initialize()
        {
            ConfigurationChangeFileWatcher.SetDefaultPollDelayInMilliseconds(1000);

            configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            configurationSettings = new ConfigurationSettings();
            ConfigurationSectionData sectionA = new ConfigurationSectionData(sectionAName);

            configurationSettings.ConfigurationSections.Add(sectionA);

            sectionA.Transformer          = new XmlSerializerTransformerData(sectionAName);
            sectionA.Transformer.TypeName = typeof(MockTransformer).AssemblyQualifiedName;

            XmlFileStorageProviderData storageProviderData = new XmlFileStorageProviderData(fooName, fooConfig);

            sectionA.StorageProvider = storageProviderData;

            ConfigurationSectionData sectionB = new ConfigurationSectionData("SectionB");

            configurationSettings.ConfigurationSections.Add(sectionB);

            sectionB.Transformer          = new XmlSerializerTransformerData(sectionAName);
            sectionB.Transformer.TypeName = typeof(XmlSerializerTransformer).AssemblyQualifiedName;

            XmlFileStorageProviderData fooStorageProviderData = new XmlFileStorageProviderData(fooName, fooConfig);

            sectionB.StorageProvider = fooStorageProviderData;
        }
 void IDisposable.Dispose()
 {
     if (this.configWatcher != null)
     {
         this.configWatcher.Dispose();
         this.configWatcher = null;
     }
 }
 private void SetUpWatcher(int refreshInterval, ConfigurationChangedEventHandler changed)
 {
     this.configWatcher =
         new ConfigurationChangeFileWatcher(
             GetFullFileName(this.configurationFilepath, this.ConfigSource),
             this.ConfigSource);
     this.configWatcher.SetPollDelayInMilliseconds(refreshInterval);
     this.configWatcher.ConfigurationChanged += changed;
 }
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (this.configWatcher != null)
         {
             this.configWatcher.Dispose();
             this.configWatcher = null;
         }
     }
 }
        public void NotifiesNothingIfNoOneListeningAndFileChanges()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@".\MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.StartWatching();

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }
        }
        public void RemoveWatcherForConfigSource(String configSource)
        {
            ConfigurationChangeFileWatcher watcher = null;

            configSourceWatcherMapping.TryGetValue(configSource, out watcher);

            if (watcher != null)
            {
                configSourceWatcherMapping.Remove(configSource);
                watcher.Dispose();
            }
        }
示例#12
0
        public void WatcherForLocalSectionIsCreatedAtStartup()
        {
            ConfigurationSection           section1 = configuration.GetSection("local.section.1");
            ConfigurationChangeFileWatcher mainWatcher
                = coordinator.ConfigSourceWatcherMapping[ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource];

            coordinator.SetWatcherForConfigSource(section1.SectionInformation.ConfigSource);

            Assert.IsTrue(coordinator.IsWatchingConfigSource(section1.SectionInformation.ConfigSource));
            Assert.AreSame(mainWatcher,
                           coordinator.ConfigSourceWatcherMapping[ConfigurationChangeWatcherCoordinator.MainConfigurationFileSource]);
        }
        public void StoppingPollingDoesNotThrowException()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(PollingThreadExceptionCatcher);
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher("MyFile.Test", "MySection"))
            {
                watcher.StartWatching();
                Thread.Sleep(100);
                watcher.StopWatching();
                Thread.Sleep(1000);
            }
            AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(PollingThreadExceptionCatcher);

            Assert.IsNull(pollingException);
        }
        public void WillStopNotifyingAfterObjectIsDisposed()
        {
            ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher("MyFile.Test", "MySection");

            watcher.SetPollDelayInMilliseconds(100);
            watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
            watcher.StartWatching();
            Thread.Sleep(100);
            watcher.Dispose();

            File.SetLastWriteTime("MyFile.Test", DateTime.Now + TimeSpan.FromHours(1.0));
            Thread.Sleep(250);

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void NoCallbackWillHappenOnFileDelete()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();

                File.Delete("MyFile.Test");

                Thread.Sleep(250);
            }

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void NoCallbackWillHappenAfterLastEventHandlerIsRemoved()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(DifferentFileChangedCallback);
                watcher.StartWatching();
                watcher.ConfigurationChanged -= new ConfigurationChangedEventHandler(DifferentFileChangedCallback);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(0, configurationChangeCounter);
        }
示例#17
0
        private void SetTracingFlag(bool tracingEnabled)
        {
            ConfigurationChangeFileWatcher.SetDefaultPollDelayInMilliseconds(100);
            SystemConfigurationSource.ResetImplementation(true);
            Logger.Reset();

            SysConfig.Configuration config = SysConfig.ConfigurationManager.OpenExeConfiguration(SysConfig.ConfigurationUserLevel.None);
            Logging.Configuration.LoggingSettings loggingSettings = config.GetSection(Logging.Configuration.LoggingSettings.SectionName) as Logging.Configuration.LoggingSettings;
            loggingSettings.TracingEnabled = tracingEnabled;
            config.Save();

            SysConfig.ConfigurationManager.RefreshSection(Logging.Configuration.LoggingSettings.SectionName);
            SystemConfigurationSource.Implementation.ConfigSourceChanged(string.Empty);

            Thread.Sleep(200);
        }
        public void StopsNotifyingHandlersWhenAnyHandlerThrowsException()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(ExceptionThrowingCallback);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(1, configurationChangeCounter);
        }
        public void NotifiesMultipleListenersIfFileChanges()
        {
            string fullyQualifiedPath = Path.GetFullPath("MyFile.Test");

            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(fullyQualifiedPath, "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(2, configurationChangeCounter);
        }
        public void NotificationsDoNotAccumulateWhileCallbacksAreHappening()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(300);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                ChangeFile("MyFile.test");
                Thread.Sleep(50);

                ChangeFile("MyFile.test");
                Thread.Sleep(450);
            }

            Assert.AreEqual(1, configurationChangeCounter);
        }
        public void CreatingNewWatchedFileDoesNotCauseCallback()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();

                using (FileStream differentFile = new FileStream("Different.Test", FileMode.Create))
                {
                    Thread.Sleep(50);
                    differentFile.Close();
                    Thread.Sleep(250);
                }
            }

            File.Delete("Different.Test");

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void CreatingNewWatchedFileDoesNotCauseCallback()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();

                using (FileStream differentFile = new FileStream("Different.Test", FileMode.Create))
                {
                    Thread.Sleep(50);
                    differentFile.Close();
                    Thread.Sleep(250);
                }
            }

            File.Delete("Different.Test");

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void NotifiesSingleListenerIfFileChanges()
        {
            string fullyQualifiedPath = Path.GetFullPath("MyFile.Test");
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(1, configurationChangeCounter);
            Assert.AreEqual("MySection", storedEventArgs.SectionName);
            Assert.AreEqual(fullyQualifiedPath.ToLower(), storedEventArgs.ConfigurationFile.ToLower());
        }
        public void NoCallbackWillHappenAfterLastEventHandlerIsRemoved()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(DifferentFileChangedCallback);
                watcher.StartWatching();
                watcher.ConfigurationChanged -= new ConfigurationChangedEventHandler(DifferentFileChangedCallback);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void OverwritingExistingWatchedFileCausesOnlyOneCallback()
        {
            if (File.Exists("Different.Test"))
            {
                File.Delete("Different.Test");
            }
            using (FileStream differentFile = new FileStream("Different.Test", FileMode.CreateNew))
            {
                differentFile.Close();
            }

            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"Different.Test", "MySection"))
            {
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.SetPollDelayInMilliseconds(100);
                watcher.StartWatching();
                Thread.Sleep(100);

                ChangeFile("Different.Test");
                Thread.Sleep(250);
            }

            File.Delete("Different.Test");

            Assert.AreEqual(1, configurationChangeCounter);
        }
示例#26
0
        public IConfigurationChangeWatcher CreateFileWatcher()
        {
            ConfigurationChangeFileWatcher fileWatcher = new ConfigurationChangeFileWatcher(configurationFile, ConfigurationSettings.SectionName);

            return(fileWatcher);
        }
 public void Initialize()
 {
     configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
     context    = new ConfigurationContext(new DisposingWrapper(new ConfigurationBuilder(configFile)));
     ConfigurationChangeFileWatcher.SetDefaultPollDelayInMilliseconds(100);
 }
 public void Reset()
 {
     context.Dispose();
     ConfigurationChangeFileWatcher.ResetDefaultPollDelay();
 }
        public void StoppingPollingDoesNotThrowException()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(PollingThreadExceptionCatcher);
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher("MyFile.Test", "MySection"))
            {
                watcher.StartWatching();
                Thread.Sleep(100);
                watcher.StopWatching();
                Thread.Sleep(1000);
            }
            AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(PollingThreadExceptionCatcher);

            Assert.IsNull(pollingException);
        }
        public void NotificationsDoNotAccumulateWhileCallbacksAreHappening()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(300);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                ChangeFile("MyFile.test");
                Thread.Sleep(50);

                ChangeFile("MyFile.test");
                Thread.Sleep(450);
            }

            Assert.AreEqual(1, configurationChangeCounter);
        }
 public void ResetStuff()
 {
     ConfigurationChangeFileWatcher.ResetDefaultPollDelay();
 }
        public void StopsNotifyingHandlersWhenAnyHandlerThrowsException()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(ExceptionThrowingCallback);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();
                Thread.Sleep(100);

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }

            Assert.AreEqual(1, configurationChangeCounter);
        }
        public void WillStopNotifyingAfterObjectIsDisposed()
        {
            ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher("MyFile.Test", "MySection");
            watcher.SetPollDelayInMilliseconds(100);
            watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
            watcher.StartWatching();
            Thread.Sleep(100);
            watcher.Dispose();

            File.SetLastWriteTime("MyFile.Test", DateTime.Now + TimeSpan.FromHours(1.0));
            Thread.Sleep(250);

            Assert.AreEqual(0, configurationChangeCounter);
        }
        public void NotifiesNothingIfNoOneListeningAndFileChanges()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@".\MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.StartWatching();

                File.SetLastWriteTime("MyFile.test", DateTime.Now + TimeSpan.FromHours(1.0));
                Thread.Sleep(250);
            }
        }
        public void NoCallbackWillHappenOnFileDelete()
        {
            using (ConfigurationChangeFileWatcher watcher = new ConfigurationChangeFileWatcher(@"MyFile.Test", "MySection"))
            {
                watcher.SetPollDelayInMilliseconds(100);
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(FileChanged);
                watcher.StartWatching();

                File.Delete("MyFile.Test");

                Thread.Sleep(250);
            }

            Assert.AreEqual(0, configurationChangeCounter);
        }