Exemplo n.º 1
0
        /// <summary>
        /// This method reads the secrets from appsettings.json and sets them
        /// AppSettings are set in environment variables, ConfigurationManager.AppSettings
        /// ConnectionStrings are only set in ConfigurationManager.ConnectionStrings
        ///
        /// It also sets up a FileSystemWatcher that kills the current running process
        /// when appsettings.json is updated.
        /// </summary>
        private void ReadSecrets()
        {
            try
            {
                var secretsManager = new SecretsManager();
                var secrets        = secretsManager.GetSecrets();
                UpdateEnvironmentVariables(secrets);
                UpdateAppSettings(secrets);
                UpdateConnectionStrings(secretsManager.GetConnectionStrings());
            }
            catch (Exception e)
            {
                if (Environment.GetEnvironmentVariable(Constants.CliDebug) == "1")
                {
                    ColoredConsole.Error.WriteLine(WarningColor(e.ToString()));
                }
                else
                {
                    ColoredConsole.Error.WriteLine(WarningColor(e.Message));
                }
            }

            fsWatcher          = new FileSystemWatcher(Environment.CurrentDirectory, SecretsManager.AppSettingsFileName);
            fsWatcher.Changed += (s, e) =>
            {
                Environment.Exit(ExitCodes.Success);
            };
            fsWatcher.EnableRaisingEvents = true;
        }