Esempio n. 1
0
        /// <summary>
        /// 初始化DI容器
        /// 自动装载被标识的对象
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="dISetting"></param>
        public static void InitDI(IServiceCollection serviceCollection, DISetting dISetting)
        {
            serviceCollection.AddHttpClient();

            DIContainerContainer.DIContainer = new DIContainerDefault(serviceCollection, serviceCollection.BuildServiceProvider());
            DIContainerInit.Init             = new DIContainerInitDefault();
            DIContainerInit.Execute(dISetting.SearchAssemblyNames);



            //装载需要手动处理的DI数据

            //Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository
            //DIContainerContainer.Inject<IXmlRepository, DataProtectionXmlRepository>(@"Configurations\Dataprotection\key.xml");
        }
Esempio n. 2
0
        /// <summary>
        /// 初始化DI容器
        /// 自动装载被标识的对象
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="dISetting"></param>
        public static void InitDI(IServiceCollection services, ContainerBuilder containerBuilder, DISetting dISetting)
        {
            //services.AddHttpClient();
            //var serviceProvider = services.BuildServiceProvider();
            //containerBuilder.RegisterInstance(serviceProvider.GetService<IHttpClientFactory>()).As<IHttpClientFactory>();

            DIContainerForAutofac diContainer = new DIContainerForAutofac(containerBuilder);

            DIContainerContainer.DIContainer = diContainer;


            DIContainerInit.Init = new DIContainerInitDefault();
            DIContainerInit.Execute(dISetting.SearchAssemblyNames);

            containerBuilder.RegisterBuildCallback((container) =>
            {
                diContainer.CompleteInit(container.BeginLifetimeScope());

                //初始化静态设置
                MainStartupHelper.InitStaticInfo();
                InitStaticInfo();

                //配置日志工厂
                var loggerFactory = LoggerFactory.Create((builder) =>
                {
                    MainStartupHelper.InitLogger(builder);
                });
                DIContainerContainer.Inject <ILoggerFactory>(loggerFactory);
            });


            //装载需要手动处理的DI数据

            //Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository
            //DIContainerContainer.Inject<IXmlRepository, DataProtectionXmlRepository>(@"Configurations\Dataprotection\key.xml");
        }