Пример #1
0
        private static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile)
        {
            configurationFile = Path.GetFullPath(configurationFile);

            var multiValuedArguments = typeof(Options)
                                       .GetPropertiesRecursively()
                                       .Where(p => p.PropertyType.IsArray)
                                       .SelectMany(p =>
                                                   p.CustomAttributes
                                                   .Where(a => a.AttributeType == typeof(ArgumentAttribute))
                                                   .Select(a => new[] { a.ConstructorArguments[0].Value, a.ConstructorArguments[1].Value })
                                                   .SelectMany(v => v))
                                       .Select(v => v.ToString())
                                       .Where(v => v != "\u0000")
                                       .ToArray();

            return(builder
                   .AddDefaultValues(
                       targetType: typeof(Options))
                   .AddEnvironmentVariables(
                       targetType: typeof(Options),
                       prefix: environmentVariablePrefix)
                   .AddYamlFile(
                       path: Path.GetFileName(configurationFile),
                       targetType: typeof(Options),
                       optional: true,
                       reloadOnChange: true,
                       provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory
                   .AddCommandLine(
                       targetType: typeof(Options),
                       multiValuedArguments,
                       commandLine: Environment.CommandLine));
        }
Пример #2
0
        /// <summary>
        ///     Adds a default value configuration source to <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
        /// <param name="targetType">The type from which to load default values.</param>
        /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, Type targetType)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            return(builder.AddDefaultValues(s =>
            {
                s.TargetType = targetType;
            }));
        }
Пример #3
0
        private static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile)
        {
            configurationFile = Path.GetFullPath(configurationFile);

            return(builder
                   .AddDefaultValues(
                       targetType: typeof(Options))
                   .AddEnvironmentVariables(
                       targetType: typeof(Options),
                       prefix: environmentVariablePrefix)
                   .AddYamlFile(
                       path: Path.GetFileName(configurationFile),
                       optional: true,
                       reloadOnChange: false,
                       provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory
                   .AddCommandLine(
                       targetType: typeof(Options),
                       commandLine: Environment.CommandLine));
        }