コード例 #1
0
        /// <summary>
        /// Use the KickStart extension to configure Microsoft.Extensions.DependencyInjection.
        /// </summary>
        /// <param name="configurationBuilder">The configuration builder.</param>
        /// <param name="configure">The <see langword="delegate"/> to configure DependencyInjection options.</param>
        /// <returns>
        /// A fluent <see langword="interface"/> to configure KickStart.
        /// </returns>
        public static IConfigurationBuilder UseDependencyInjection(this IConfigurationBuilder configurationBuilder, Action <IDependencyInjectionBuilder> configure)
        {
            var options = new DependencyInjectionOptions();
            var starter = new DependencyInjectionStarter(options);

            if (configure != null)
            {
                var builder = new DependencyInjectionBuilder(options);
                configure(builder);
            }

            configurationBuilder.ExcludeAssemblyFor <DependencyInjectionStarter>();
            configurationBuilder.ExcludeAssemblyFor <IServiceCollection>();
            configurationBuilder.Use(starter);

            return(configurationBuilder);
        }
コード例 #2
0
        /// <summary>
        /// 使用依赖注入(注入继承<see cref="ControllerBase"/>的构造函数参数类型,也会注入【继承<see cref="ControllerBase"/>的构造函数参数】以及【其参数类型的构造函数参数】)。
        /// </summary>
#else
        /// <summary>
        /// 使用依赖注入(注入继承<see cref="ApiController"/>的构造函数参数类型,也会注入【继承<see cref="ApiController"/>的构造函数参数】以及【其参数类型的构造函数参数】)。
        /// </summary>
#endif
        public static IServiceCollection UseDependencyInjection(this IServiceCollection services, DependencyInjectionOptions options)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            string pattern = options.Pattern;

            var assemblys = AssemblyFinder.Find(pattern);

            var assemblyTypes = assemblys
                                .SelectMany(x => x.GetTypes())
                                .Where(x => x.IsClass || x.IsInterface)
                                .ToList();

#if NETCOREAPP2_0_OR_GREATER
            var controllerBaseType = typeof(ControllerBase);
#else
            var controllerBaseType = typeof(ApiController);
#endif

            var controllerTypes = assemblyTypes
                                  .Where(x => x.IsClass && !x.IsAbstract && controllerBaseType.IsAssignableFrom(x))
                                  .ToList();

            int             maxDepth = options.MaxDepth;
            ServiceLifetime lifetime = options.Lifetime;

            if (!Enum.IsDefined(typeof(ServiceLifetime), lifetime))
            {
                throw new InvalidCastException($"“{lifetime}”不是有效的“ServiceLifetime”枚举值!");
            }

            foreach (var controllerType in controllerTypes)
            {
                bool flag = false;

                foreach (var constructorInfo in controllerType.GetConstructors().Where(x => x.IsPublic))
                {
                    flag = true;

                    foreach (var parameterInfo in constructorInfo.GetParameters())
                    {
                        if (parameterInfo.IsOptional || Di(services, parameterInfo.ParameterType, assemblyTypes, 0, maxDepth, lifetime))
                        {
                            continue;
                        }

                        flag = false;

                        break;
                    }

                    if (flag)
                    {
                        break;
                    }
                }

                if (flag)
                {
                    continue;
                }

                throw new TypeLoadException($"Controller '{controllerType.FullName}' cannot be created and the current maximum dependency injection depth is {maxDepth}.");
            }

            return(services);
        }