Ejemplo n.º 1
0
        public static void LoadSettings(string settingsFilePath, string environmentName = null, string machineName = null, string applicationName = null)
        {
            if (settingsFilePath == null)
            {
                throw new NullReferenceException("The settings file path is null.");
            }

            environmentName = environmentName ?? EnvironmentName;
            machineName     = machineName ?? MachineName;
            applicationName = applicationName ?? ApplicationName;

            settingsFilePath = settingsFilePath?.Replace("{EnvironmentName}", environmentName);
            settingsFilePath = settingsFilePath?.Replace("{MachineName}", machineName);
            settingsFilePath = settingsFilePath?.Replace("{ApplicationName}", applicationName);
            Ensure.ExistFile(settingsFilePath);
            var serializer = SerializerManager.GetByFileExtension(Path.GetExtension(settingsFilePath));

            Ensure.ReferenceNotNull(serializer, $"A serializer for file '{settingsFilePath}' was not found");
            GlobalSettings globalSettings;

            try
            {
                if (serializer is ITextSerializer txtSerializer)
                {
                    var fileContent = File.ReadAllText(settingsFilePath);
                    fileContent    = ReplaceEnvironmentTemplate(fileContent);
                    globalSettings = txtSerializer.DeserializeFromString <GlobalSettings>(fileContent);
                }
                else
                {
                    globalSettings = serializer.DeserializeFromFile <GlobalSettings>(settingsFilePath);
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"The Global settings file: {settingsFilePath} can't be deserialized.", ex);
            }
            try
            {
                LoadSettings(globalSettings, environmentName, machineName, applicationName);
                RebindSettings();
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading the settings definitions.", ex);
            }
            //Checks if a reload time is set.
            if (GlobalSettings != null && GlobalSettings.SettingsReloadTimeInMinutes > 0)
            {
                Task.Delay(GlobalSettings.SettingsReloadTimeInMinutes * 60 * 1000).ContinueWith(t =>
                {
                    LoadSettings(settingsFilePath, environmentName, machineName, applicationName);
                });
            }
        }
Ejemplo n.º 2
0
        public static void LoadInjector(string injectorFilePath, string environmentName = null, string machineName = null, string applicationName = null)
        {
            if (injectorFilePath == null)
            {
                throw new NullReferenceException("The injector file path is null.");
            }

            environmentName = environmentName ?? EnvironmentName;
            machineName     = machineName ?? MachineName;
            applicationName = applicationName ?? ApplicationName;

            injectorFilePath = injectorFilePath?.Replace("{EnvironmentName}", environmentName);
            injectorFilePath = injectorFilePath?.Replace("{MachineName}", machineName);
            injectorFilePath = injectorFilePath?.Replace("{ApplicationName}", applicationName);
            Ensure.ExistFile(injectorFilePath);
            var serializer = SerializerManager.GetByFileExtension(Path.GetExtension(injectorFilePath));

            Ensure.ReferenceNotNull(serializer, $"A serializer for file '{injectorFilePath}' was not found");
            InjectorGlobalSettings globalSettings;

            try
            {
                if (serializer is ITextSerializer txtSerializer)
                {
                    var fileContent = File.ReadAllText(injectorFilePath);
                    fileContent    = ReplaceEnvironmentTemplate(fileContent);
                    globalSettings = txtSerializer.DeserializeFromString <InjectorGlobalSettings>(fileContent);
                }
                else
                {
                    globalSettings = serializer.DeserializeFromFile <InjectorGlobalSettings>(injectorFilePath);
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"The Injector settings file: {injectorFilePath} can't be deserialized.", ex);
            }
            try
            {
                LoadInjector(globalSettings, environmentName, applicationName);
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading the injector definitions.", ex);
            }
        }