/// <summary>
        ///     Get a application from the service collection.
        ///     This method builds the <see cref="ServiceProvider" /> collection. Because of this behavior, the options must be registered in the
        ///     <see cref="IServiceCollection" /> prior to calling this method.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="settingsSection"></param>
        /// <typeparam name="TOptions"></typeparam>
        /// <returns></returns>
        public static TOptions GetOptions <TOptions>(this IServiceCollection services, string settingsSection)
            where TOptions : class, new()
        {
            using ServiceProvider serviceProvider = services.BuildServiceProvider();
            var configuration = serviceProvider.GetService <IConfiguration>();

            return(BinderHelper.BindOptions <TOptions>(configuration.GetSection(settingsSection)));
        }
Example #2
0
 /// <summary>
 ///     Gets an section of the json configuration and binds to an object.
 /// </summary>
 /// <param name="configuration">
 ///     <see cref="IConfiguration" />
 /// </param>
 /// <param name="section">Configuration section name</param>
 /// <typeparam name="TOptions">Resulting object</typeparam>
 /// <returns>Instance of TOptions</returns>
 public static TOptions GetOptions <TOptions>(this IConfigurationSection section)
     where TOptions : class, new()
 {
     return(BinderHelper.BindOptions <TOptions>(section));
 }
Example #3
0
 /// <summary>
 ///     Gets an section of the json configuration and binds to an object.
 /// </summary>
 /// <param name="configuration">
 ///     <see cref="IConfiguration" />
 /// </param>
 /// <param name="section">Configuration section name</param>
 /// <typeparam name="TOptions">Resulting object</typeparam>
 /// <returns>Instance of TOptions</returns>
 public static TOptions GetOptions <TOptions>(this IConfiguration configuration, string section)
     where TOptions : class, new()
 {
     return(BinderHelper.BindOptions <TOptions>(configuration.GetSection(section)));
 }