From b1549f00b60cf0e9730c066eb037915e1ba05037 Mon Sep 17 00:00:00 2001 From: "454313500@qq.com" <454313500@qq.com> Date: Sun, 28 Mar 2021 20:56:27 +0800 Subject: [PATCH] v1.0.9 --- CC.Yi.API/Program.cs | 8 ++++---- CC.Yi.API/Startup.cs | 11 +++++++++++ CC.Yi.DAL/DbContentFactory.cs | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CC.Yi.API/Program.cs b/CC.Yi.API/Program.cs index 06ecf46f..9fe2b1eb 100644 --- a/CC.Yi.API/Program.cs +++ b/CC.Yi.API/Program.cs @@ -22,10 +22,10 @@ namespace CC.Yi.API { logger.Debug("正在启动Yi意框架。。。。。。"); var host = CreateHostBuilder(args).Build(); - var scope = host.Services.CreateScope(); - var services = scope.ServiceProvider; - var context = services.GetRequiredService();//获取服务 - DbContentFactory.Initialize(context);//调用静态类方法注入 + //var scope = host.Services.CreateScope(); + //var services = scope.ServiceProvider; + //var context = services.GetRequiredService();//获取服务 + //DbContentFactory.Initialize(context);//调用静态类方法注入 host.Run(); logger.Info("Yi意框架启动成功!"); } diff --git a/CC.Yi.API/Startup.cs b/CC.Yi.API/Startup.cs index 0a0989e0..9ffc7e32 100644 --- a/CC.Yi.API/Startup.cs +++ b/CC.Yi.API/Startup.cs @@ -75,11 +75,21 @@ namespace CC.Yi.API }).AddEntityFrameworkStores().AddDefaultTokenProviders(); } + private void InitData(IServiceProvider serviceProvider) + { + var serviceScope = serviceProvider.GetRequiredService().CreateScope(); + + var context = serviceScope.ServiceProvider.GetService(); + DbContentFactory.Initialize(context);//调用静态类方法注入 + + } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -99,6 +109,7 @@ namespace CC.Yi.API { endpoints.MapControllers(); }); + InitData(app.ApplicationServices); } } } diff --git a/CC.Yi.DAL/DbContentFactory.cs b/CC.Yi.DAL/DbContentFactory.cs index 9ca47789..2ef1c920 100644 --- a/CC.Yi.DAL/DbContentFactory.cs +++ b/CC.Yi.DAL/DbContentFactory.cs @@ -9,6 +9,8 @@ namespace CC.Yi.DAL public class DbContentFactory { private static DataContext Webcontext; + + public static void Initialize(DataContext webContext) { Webcontext = webContext;