Esempio n. 1
0
 public static OptionsBuilder <TOptions> Bind <TOptions>(this OptionsBuilder <TOptions> optionsBuilder,
                                                         IConfigurationSection section)
     where TOptions : class
 {
     return(optionsBuilder
            .Configure(section.Bind));
 }
Esempio n. 2
0
 public static OptionsBuilder <TOptions> UseOptions <TOptions, TOptionsDep>(
     this OptionsBuilder <TOptions> optionsBuilder, Action <TOptions, TOptionsDep> configureOptions)
     where TOptionsDep : class where TOptions : class
 {
     return(optionsBuilder
            .Configure <IOptionsMonitor <TOptionsDep> >((options, dependency) =>
                                                        configureOptions(options, dependency.Get(optionsBuilder.Name))));
 }
        public static OptionsBuilder <TOptions> ConfigureSection <TOptions>(this OptionsBuilder <TOptions> builder)
            where TOptions : class
        {
            var section = typeof(TOptions).Name.Replace(Options, string.Empty);

            return(builder.Configure <IConfiguration>((options, configuration) =>
                                                      configuration.GetSection(section).Bind(options)));
        }
        public static OptionsBuilder <TOption> SubstituteVariables <TOption>(this OptionsBuilder <TOption> builder) where TOption : class
        {
            builder.Services.TryAddSingleton <IVariablesSubstitution <string>, EnvironmentVariablesSubstitution>();
            builder.Services.TryAddSingleton <IOptionConfigurator, OptionConfigurator>();

            builder.Configure <IOptionConfigurator>((opt, conf) =>
            {
                conf.Configure(opt);
            });
            return(builder);
        }
Esempio n. 5
0
        public static OptionsBuilder <TOptions> BindConfiguration <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>(
            this OptionsBuilder <TOptions> optionsBuilder,
            string configSectionPath,
            Action <BinderOptions> configureBinder = null)
            where TOptions : class
        {
            _ = optionsBuilder ?? throw new ArgumentNullException(nameof(optionsBuilder));
            _ = configSectionPath ?? throw new ArgumentNullException(nameof(configSectionPath));

            optionsBuilder.Configure <IConfiguration>((opts, config) => BindFromOptions <TOptions>(opts, config, configSectionPath, configureBinder));
            return(optionsBuilder);
        }
        /// <summary>
        /// Registers an action used to configure a particular type of options and validates this options instance at startup.
        /// Note: These are run before all <seealso cref="PostConfigure{TOptions}(IServiceCollection, Action{TOptions})"/>.
        /// </summary>
        /// <typeparam name="TOptions">The options type to be configured.</typeparam>
        /// <param name="optionsBuilder">The options builder to add the services to.</param>
        /// <param name="configureOptions">The action used to configure the options.</param>
        /// <returns>The <see cref="OptionsBuilder{TOptions}"/> so that additional calls can be chained.</returns>
        public static OptionsBuilder <TOptions> ConfigureAndValidate <TOptions>(this OptionsBuilder <TOptions> optionsBuilder, Action <TOptions> configureOptions) where TOptions : class
        {
            if (optionsBuilder == null)
            {
                throw new ArgumentNullException(nameof(optionsBuilder));
            }

            return(optionsBuilder
                   .Configure(configureOptions)
                   .ValidateDataAnnotationsRecursively()
                   .ValidateOnStart());
        }
Esempio n. 7
0
        public static OptionsBuilder <TOptions> BindConfiguration <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>(
            this OptionsBuilder <TOptions> optionsBuilder,
            string configSectionPath,
            Action <BinderOptions>?configureBinder = null)
            where TOptions : class
        {
            ThrowHelper.ThrowIfNull(optionsBuilder);
            ThrowHelper.ThrowIfNull(configSectionPath);

            optionsBuilder.Configure <IConfiguration>((opts, config) => BindFromOptions <TOptions>(opts, config, configSectionPath, configureBinder));
            optionsBuilder.Services.AddSingleton <IOptionsChangeTokenSource <TOptions>, ConfigurationChangeTokenSource <TOptions> >();
            return(optionsBuilder);
        }
        public static OptionsBuilder <TClientOptions> WithSerializer <TClientOptions>(
            this OptionsBuilder <TClientOptions> optionsBuilder,
            ISerializer serializer)
            where TClientOptions : BaseClientOptions
        {
            if (optionsBuilder == null)
            {
                throw new ArgumentNullException(nameof(optionsBuilder));
            }
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            return(optionsBuilder.Configure(x => x.Serializer = serializer));
        }
 public static OptionsBuilder <TOptions> BindCommandLine <TOptions>(
     this OptionsBuilder <TOptions> optionsBuilder)
     where TOptions : class
 {
     if (optionsBuilder is null)
     {
         throw new ArgumentNullException(nameof(optionsBuilder));
     }
     return(optionsBuilder.Configure <IServiceProvider>((opts, serviceProvider) =>
     {
         var modelBinder = serviceProvider
                           .GetService <ModelBinder <TOptions> >()
                           ?? new ModelBinder <TOptions>();
         var bindingContext = serviceProvider.GetRequiredService <BindingContext>();
         modelBinder.UpdateInstance(opts, bindingContext);
     }));
 }
Esempio n. 10
0
        /// <summary>
        /// Registers the dependency injection container to bind <typeparamref name="TOptions"/> against
        /// the <see cref="IConfiguration"/> obtained from the DI service provider.
        /// </summary>
        /// <typeparam name="TOptions">The options type to be configured.</typeparam>
        /// <param name="optionsBuilder">The options builder to add the services to.</param>
        /// <param name="configSectionPath">The name of the configuration section to bind from.</param>
        /// <param name="configureBinder">Optional. Used to configure the <see cref="BinderOptions"/>.</param>
        /// <returns>The <see cref="OptionsBuilder{TOptions}"/> so that additional calls can be chained.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="optionsBuilder"/> or <paramref name="configSectionPath" /> is <see langword="null"/>.
        /// </exception>
        /// <seealso cref="Bind{TOptions}(OptionsBuilder{TOptions}, IConfiguration, Action{BinderOptions})"/>
        public static OptionsBuilder <TOptions> BindConfiguration <TOptions>(
            this OptionsBuilder <TOptions> optionsBuilder,
            string configSectionPath,
            Action <BinderOptions> configureBinder = null)
            where TOptions : class
        {
            _ = optionsBuilder ?? throw new ArgumentNullException(nameof(optionsBuilder));
            _ = configSectionPath ?? throw new ArgumentNullException(nameof(configSectionPath));

            optionsBuilder.Configure <IConfiguration>((opts, config) =>
            {
                IConfiguration section = string.Equals("", configSectionPath, StringComparison.OrdinalIgnoreCase)
                    ? config
                    : config.GetSection(configSectionPath);
                section.Bind(opts, configureBinder);
            });
            return(optionsBuilder);
        }
        public static OptionsBuilder <TOptions> BindConfiguration <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>(
            this OptionsBuilder <TOptions> optionsBuilder,
            string configSectionPath,
            Action <BinderOptions>?configureBinder = null)
            where TOptions : class
        {
            ThrowHelper.ThrowIfNull(optionsBuilder);
            ThrowHelper.ThrowIfNull(configSectionPath);

            optionsBuilder.Configure <IConfiguration>((opts, config) =>
            {
                IConfiguration section = string.Equals("", configSectionPath, StringComparison.OrdinalIgnoreCase)
                    ? config
                    : config.GetSection(configSectionPath);
                section.Bind(opts, configureBinder);
            });
            optionsBuilder.Services.AddSingleton <IOptionsChangeTokenSource <TOptions>, ConfigurationChangeTokenSource <TOptions> >();
            return(optionsBuilder);
        }
Esempio n. 12
0
 public static OptionsBuilder <CoinbaseApiOptions> UseProduction(this OptionsBuilder <CoinbaseApiOptions> configureOptions)
 => configureOptions.Configure(o =>
 {
     o.RestApiBaseUri   = EndpointUriNames.RestApiUri;
     o.WebsocketFeedUri = EndpointUriNames.WebsocketFeedUri;
 });