Exemplo n.º 1
0
        /// <summary>
        /// 直接获取选项配置
        /// <para>该方式未通过依赖注入实现,无法使用热更新</para>
        /// <para></para>
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="configuation"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static TOptions GetDefultOptions <TOptions>(this IConfiguration configuation, string key = null)
            where TOptions : IConfigurableOptions, new()
        {
            var optionsType     = typeof(TOptions);
            var optionsSettings = optionsType.GetCustomAttribute <OptionsSettingsAttribute>(false);

            // 获取键名
            var jsonKey = JsonKey.GetOptionsJsonKey(optionsSettings, optionsType);

            return(configuation.GetSection(jsonKey).Get <TOptions>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加选项配置
        /// </summary>
        /// <typeparam name="TOptions">选项类型</typeparam>
        /// <param name="services">服务集合</param>
        /// <param name="key"></param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddConfigurableOptions <TOptions>(this IServiceCollection services, string key = null)
            where TOptions : class, IConfigurableOptions
        {
            var optionsType     = typeof(TOptions);
            var optionsSettings = optionsType.GetCustomAttribute <OptionsSettingsAttribute>(false);

            // 获取键名
            var jsonKey = JsonKey.GetOptionsJsonKey(optionsSettings, optionsType);
            // 配置选项(含验证信息)
            var configurationRoot = App.Configuration;

            if (configurationRoot == null)
            {
                throw new QingShanException("您需要在服务中添加`services.AddConfigurable(Configuration);`");
            }
            var optionsConfiguration = configurationRoot.GetSection(key ?? jsonKey);

            services.Configure <TOptions>(optionsConfiguration);


            // 配置复杂验证后后期配置
            var validateInterface = optionsType.GetInterfaces()
                                    .FirstOrDefault(u => u.IsGenericType && typeof(IConfigurableOptions).IsAssignableFrom(u.GetGenericTypeDefinition()));

            if (validateInterface != null)
            {
                var genericArguments = validateInterface.GenericTypeArguments;

                // 配置复杂验证
                if (genericArguments.Length > 1)
                {
                    services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IValidateOptions <TOptions>), genericArguments.Last()));
                }

                // 配置后期配置
                var postConfigureMethod = optionsType.GetMethod(nameof(IConfigurableOptions <TOptions> .PostConfigure));
                if (postConfigureMethod != null)
                {
                    if (optionsSettings?.PostConfigureAll != true)
                    {
                        services.PostConfigure <TOptions>(options => postConfigureMethod.Invoke(options, new object[] { options, optionsConfiguration }));
                    }
                    else
                    {
                        services.PostConfigureAll <TOptions>(options => postConfigureMethod.Invoke(options, new object[] { options, optionsConfiguration }));
                    }
                }
            }

            return(services);
        }