public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { //获取核心配置 var coreConfiguration = ConfigurationContainer.Get <CoreConfiguration>(ConfigurationNames.Application); //初始化DI容器 MainStartupHelper.InitDI(services, coreConfiguration.DISetting); //初始化静态设置 MainStartupHelper.InitStaticInfo(); }) .ConfigureLogging((builder) => { //初始化日志配置 MainStartupHelper.InitLogger(builder); }) .ConfigureAppConfiguration((context, builder) => { //context.HostingEnvironment.EnvironmentName }) .UseConfiguration(ConfigurationContainer.GetConfiguration(ConfigurationNames.Host)) .UseStartup <Startup>();
public static void Main(string[] args) { //设置应用程序工作基目录 _baseUrl = Path.GetDirectoryName(typeof(Program).Assembly.Location); Environment.CurrentDirectory = _baseUrl ?? Environment.CurrentDirectory; //获取运行环境名称 var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_APPLICATIONNAME"); if (environmentName == null) { environmentName = string.Empty; } //初始化配置容器 MainStartupHelper.InitConfigurationContainer(environmentName, _baseUrl); //初始化上下文容器 MainStartupHelper.InitContext(); CreateWebHostBuilder(args).Build().Run(); }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseIISIntegration() .ConfigureServices((context, services) => { //初始化配置容器 StartupHelper.InitConfigurationContainer(context.HostingEnvironment.EnvironmentName, _baseUrl); //获取核心配置 var coreConfiguration = ConfigurationContainer.Get <CoreConfiguration>(ConfigurationNames.Application); //初始化DI容器 MainStartupHelper.InitDI(services, coreConfiguration.DISetting); //初始化静态设置 MainStartupHelper.InitStaticInfo(); StartupHelper.InitStaticInfo(); //配置日志工厂 var loggerFactory = LoggerFactory.Create((builder) => { MainStartupHelper.InitLogger(builder); }); DIContainerContainer.Inject <ILoggerFactory>(loggerFactory); }) .ConfigureLogging((builder) => { //初始化日志配置 //MainStartupHelper.InitLogger(builder); }) .ConfigureKestrel(opts => { }) .UseConfiguration(ConfigurationContainer.GetConfiguration(ConfigurationNames.Host)) .UseStartup <Startup>();
/// <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"); }