Пример #1
0
        public static IServiceCollection ConfigureConfigTypes(this IServiceCollection services, IConfiguration configuration)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

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

            var        assemblies  = DefaultAssemblyLocator.Locate();
            var        configTypes = ConfigSectionAttribute.GetAllAppliedTypes(assemblies);
            MethodInfo method      = typeof(OptionsConfigurationServiceCollectionExtensions)
                                     .GetMethod("Configure", new Type[] { typeof(IServiceCollection), typeof(IConfiguration) });

            foreach (var configTypeInfo in configTypes)
            {
                var configSectionAttribute = configTypeInfo.GetCustomAttribute <ConfigSectionAttribute>();
                var configSection          = configuration.GetSection(configSectionAttribute.SectionName);

                // https://github.com/aspnet/Options/blob/1.0.0-rc2/src/Microsoft.Extensions.Options.ConfigurationExtensions/OptionsServiceCollectionExtensions.cs#L12-L27
                MethodInfo genericMethod = method.MakeGenericMethod(configTypeInfo.AsType());
                genericMethod.Invoke(null, new object[] { services, configSection });
            }

            return(services);
        }
        public static TConfigType GetConfigType <TConfigType>(this IConfiguration configuration)
        {
            var assemblies  = DefaultAssemblyLocator.Locate();
            var configTypes = ConfigSectionAttribute.GetAllAppliedTypes(assemblies);
            var configType  = configTypes.FirstOrDefault(x => x.AsType() == typeof(TConfigType));

            if (configType != null)
            {
                var configSectionAttribute = configType.GetCustomAttribute <ConfigSectionAttribute>();
                var configInstance         = Activator.CreateInstance(typeof(TConfigType));
                var section = configuration.GetSection(configSectionAttribute.SectionName);

                ConfigurationBinder.Bind(section, configInstance);

                return((TConfigType)configInstance);
            }
            else
            {
                throw new InvalidOperationException($"'{typeof(TConfigType).FullName}' is not recognized as a config type.");
            }
        }
        /// <remarks>
        /// For usage of <code>ILibraryManager</code>, see: https://github.com/aspnet/SignalR-Server/blob/dae3e5331081093e038ba0d8bc74ccd8d9046a35/src/Microsoft.AspNetCore.SignalR.Server/Hubs/DefaultAssemblyLocator.cs
        /// </remarks>
        public static IEnumerable <ValidationResult> ValidateConfigTypes(this IConfiguration configuration)
        {
            var assemblies  = DefaultAssemblyLocator.Locate();
            var configTypes = ConfigSectionAttribute.GetAllAppliedTypes(assemblies);

            return(configTypes.Select(configTypeInfo =>
            {
                var configSectionAttribute = configTypeInfo.GetCustomAttribute <ConfigSectionAttribute>();
                var configInstance = Activator.CreateInstance(configTypeInfo.AsType());
                var section = configuration.GetSection(configSectionAttribute.SectionName);

                ConfigurationBinder.Bind(section, configInstance);

                if (configInstance != null)
                {
                    return Validate(configInstance);
                }
                else
                {
                    throw new InvalidOperationException($"No config values for {configTypeInfo.FullName}.");
                }
            }).SelectMany(validationResults => validationResults));
        }