/// <summary> /// Register Services dependencies<br/> /// Default is current executing assembly /// </summary> /// <param name="assembly">The assembly which contains service classes</param> public static void RegisterServicesNameSpace(this IServiceCollection serviceCollection, Assembly assembly) { // get DI setting var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json").Build(); DISetting diSetting = config.GetSection("DISettings").Get <DISetting>(); if (diSetting == null) { throw new Exception("DISetting is not defined"); } Type[] serviceTypes = ReflectionHelper.GetTypeInNamespace(diSetting.ServiceNS, assembly); Type[] implTypes = ReflectionHelper.GetTypeInNamespace(diSetting.ServiceImplNS, assembly); foreach (Type type in serviceTypes) { Type implType = implTypes.Where((t) => { bool isAssignale = type.IsAssignableFrom(t); if (diSetting != null && diSetting.UseAttribute) { Service attr = ReflectionHelper.GetServiceAttribute(t); if (attr == null) { isAssignale = false; } } return(isAssignale); }).FirstOrDefault(); if (implType == null) { Console.WriteLine("Cant find implementation for " + type.FullName); continue; } Service attr = ReflectionHelper.GetServiceAttribute(implType); if (attr == null || attr.LeftTime == LeftTime.Scope) { serviceCollection.AddScoped(type, implType); } else if (attr.LeftTime == LeftTime.Singleton) { serviceCollection.AddSingleton(type, implType); } else if (attr.LeftTime == LeftTime.Transient) { serviceCollection.AddTransient(type, implType); } } }
/// <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"); }
/// <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"); }