public static T Register <T>()
        {
            using (ChoCoreFrxConfigurationManager <T> coreFrxConfigurationManager = new ChoCoreFrxConfigurationManager <T>())
            {
                T instance = coreFrxConfigurationManager.ConfigObject;

                if (instance is IChoObjectChangeWatcheable)
                {
                    if (!ChoAppFrxSettings.Me.DisableFrxConfig)
                    {
                        ChoConfigurationChangeFileWatcher fileWatcher = null;
                        fileWatcher = new ChoConfigurationChangeFileWatcher("{0}_FileWatcher".FormatString(typeof(T).Name), coreFrxConfigurationManager.ConfigFilePath);
                        fileWatcher.DoNotUseGlobalQueue = true;
                        fileWatcher.SetConfigurationChangedEventHandler("{0}_FileWatcher".FormatString(typeof(T).Name), (sender1, e1) =>
                        {
                            using (ChoCoreFrxConfigurationManager <T> coreFrxConfigurationManager1 = new ChoCoreFrxConfigurationManager <T>())
                            {
                                T instance1 = coreFrxConfigurationManager1.ConfigObject;
                                if (instance1 is IChoObjectChangeWatcheable)
                                {
                                    ((IChoObjectChangeWatcheable)instance1).OnObjectChanged(instance1, null);
                                }
                            }
                        });

                        _dictService.AddOrUpdate(typeof(T), fileWatcher);

                        ChoEnvironmentSettings.SetEnvironmentChangedEventHandlerNoCall(typeof(T).Name, ((sender, e) =>
                        {
                            using (ChoCoreFrxConfigurationManager <T> coreFrxConfigurationManager1 = new ChoCoreFrxConfigurationManager <T>())
                            {
                                T instance1 = coreFrxConfigurationManager1.ConfigObject;
                                if (instance1 is IChoObjectChangeWatcheable)
                                {
                                    ((IChoObjectChangeWatcheable)instance1).OnObjectChanged(instance1, null);
                                }
                            }
                        }));

                        if (fileWatcher != null)
                        {
                            fileWatcher.StartWatching();
                        }
                    }
                }

                return(instance);
            }
        }
예제 #2
0
        private static void OpenConfiguration(string appConfigPath, bool doBackup)
        {
            if (_appConfigPath != appConfigPath)
            {
                Trace.TraceInformation("Using AppConfigPath: {0}".FormatString(appConfigPath));
            }

            _appConfigPath = appConfigPath;

            if (_appConfigPath.IsNullOrWhiteSpace())
            {
                Trace.TraceError("Empty AppConfigPath passed.");
                return;
            }

            ChoFile.SetReadOnly(_appConfigPath, false);

            if (_configurationChangeWatcher != null)
            {
                RestoreAppConfig();
                //_configurationChangeWatcher.StopWatching();
            }
            //if (_systemConfigurationChangeWatcher != null)
            //{
            //    _systemConfigurationChangeWatcher.StopWatching();
            //    _systemConfigurationChangeWatcher = null;
            //}

            ChoXmlDocument.CreateXmlFileIfEmpty(_appConfigPath);

            //backup the current configuration
            string backupConfigFilePath = String.Format("{0}.{1}", _appConfigPath, ChoReservedFileExt.Cho);

            try
            {
                LoadConfigurationFile();

                if (doBackup)
                {
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Archive);
                    }
                    File.Copy(_appConfigPath, backupConfigFilePath, true);
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Hidden);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                if (_appXmlDocument != null)
                {
                    _appXmlDocument.Dispose();
                    _appXmlDocument = null;
                }

                try
                {
                    //Rollback the configuration file
                    if (doBackup)
                    {
                        File.Copy(backupConfigFilePath, _appConfigPath, true);
                    }

                    LoadConfigurationFile();
                }
                catch
                {
                }
            }
            finally
            {
                if (_configurationChangeWatcher != null)
                {
                    _configurationChangeWatcher.StartWatching();
                }
                if (_systemConfigurationChangeWatcher != null)
                {
                    _systemConfigurationChangeWatcher.StartWatching();
                }
            }
        }