Exemple #1
0
        private IConfiguration SetupConfiguration()
        {
            var existingConfiguration = Builder.Services.First(z => z.ServiceType == typeof(IConfiguration))
                                        .ImplementationInstance as IConfiguration;

            var configurationBuilder = new MsftConfigurationBinder();

            configurationBuilder.AddConfiguration(existingConfiguration);

            configurationBuilder
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{_environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddYamlFile($"appsettings.{_environment.EnvironmentName}.yml", optional: true, reloadOnChange: true);

            var cb = new ConfigurationBuilder(
                Scanner,
                _environment,
                existingConfiguration,
                configurationBuilder,
                _logger,
                Properties);

            cb.Build();

            if (_environment.IsDevelopment() && !string.IsNullOrEmpty(_environment.ApplicationName))
            {
                var appAssembly = Assembly.Load(new AssemblyName(_environment.ApplicationName));
                if (appAssembly != null)
                {
                    configurationBuilder.AddUserSecrets(appAssembly, optional: true);
                }
            }

            configurationBuilder.AddEnvironmentVariables();

            var newConfig = configurationBuilder.Build();

            Builder.Services.Replace(ServiceDescriptor.Singleton <IConfiguration>(newConfig));
            return(newConfig);
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ServicesBuilder" /> class.
 /// </summary>
 /// <param name="scanner">The scanner.</param>
 /// <param name="assemblyProvider">The assembly provider.</param>
 /// <param name="assemblyCandidateFinder">The assembly candidate finder.</param>
 /// <param name="services">The services.</param>
 /// <param name="configuration">The configuration.</param>
 /// <param name="environment">The environment.</param>
 /// <param name="diagnosticSource">The diagnostic source.</param>
 /// <param name="properties">The properties.</param>
 /// <exception cref="ArgumentNullException">
 /// environment
 /// or
 /// diagnosticSource
 /// or
 /// configuration
 /// or
 /// services
 /// </exception>
 public ServicesBuilder(
     IConventionScanner scanner,
     IAssemblyProvider assemblyProvider,
     IAssemblyCandidateFinder assemblyCandidateFinder,
     IServiceCollection services,
     IConfiguration configuration,
     IRocketEnvironment environment,
     ILogger diagnosticSource,
     IDictionary <object, object?> properties
     )
     : base(scanner, assemblyProvider, assemblyCandidateFinder, properties)
 {
     Environment            = environment ?? throw new ArgumentNullException(nameof(environment));
     Configuration          = configuration ?? throw new ArgumentNullException(nameof(configuration));
     Services               = services ?? throw new ArgumentNullException(nameof(services));
     Logger                 = diagnosticSource ?? throw new ArgumentNullException(nameof(diagnosticSource));
     _onBuild               = new ServiceProviderObservable(Logger);
     ServiceProviderOptions = new ServiceProviderOptions
     {
         ValidateScopes = environment.IsDevelopment()
     };
 }
Exemple #3
0
        private IConfiguration SetupConfiguration()
        {
            var currentDirectory = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") ?? "/home/site/wwwroot";
            var isLocal          = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) &&
                                   !Directory.Exists(currentDirectory);

            if (isLocal)
            {
                currentDirectory = Environment.CurrentDirectory;
            }

            var existingConfiguration = Builder.Services.First(z => z.ServiceType == typeof(IConfiguration))
                                        .ImplementationInstance as IConfiguration;

            var configurationOptions = this.GetOrAdd(() => new ConfigurationOptions());

            var configurationBuilder = new MsftConfigurationBuilder()
                                       .SetBasePath(currentDirectory)
                                       .AddConfiguration(existingConfiguration)
                                       .Apply(configurationOptions.ApplicationConfiguration)
                                       .Apply(configurationOptions.EnvironmentConfiguration, _environment.EnvironmentName)
                                       .Apply(configurationOptions.EnvironmentConfiguration, "local");

            if (_environment.IsDevelopment())
            {
                configurationBuilder.AddUserSecrets(FunctionsAssembly, true);
            }

            configurationBuilder
            .AddEnvironmentVariables("RSG_")
            .AddEnvironmentVariables();

            IConfigurationSource?source = null;

            foreach (var item in configurationBuilder.Sources.Reverse())
            {
                if ((item is EnvironmentVariablesConfigurationSource env && (string.IsNullOrWhiteSpace(env.Prefix) ||
                                                                             string.Equals(env.Prefix, "RSG_", StringComparison.OrdinalIgnoreCase))) ||
                    (item is JsonConfigurationSource a && string.Equals(
                         a.Path,
                         "secrets.json",
                         StringComparison.OrdinalIgnoreCase
                         )))
                {
                    continue;
                }

                source = item;
                break;
            }

            var index = source == null
                ? configurationBuilder.Sources.Count - 1
                : configurationBuilder.Sources.IndexOf(source);

            var cb = new ConfigurationBuilder(
                Scanner,
                _environment,
                new MsftConfigurationBuilder().AddConfiguration(existingConfiguration !)
                .AddConfiguration(configurationBuilder.Build()).Build(),
                configurationBuilder,
                _logger,
                Properties
                );

            configurationBuilder.Sources.Insert(
                index + 1,
                new ChainedConfigurationSource
            {
                Configuration = cb.Build()
            }
                );

            var newConfig = configurationBuilder.Build();

            Builder.Services.Replace(ServiceDescriptor.Singleton <IConfiguration>(newConfig));
            return(newConfig);
        }