From 7e73d35bd2730089a0b9c25e9e3006515bac3d83 Mon Sep 17 00:00:00 2001 From: "454313500@qq.com" <454313500@qq.com> Date: Fri, 16 Apr 2021 19:21:56 +0800 Subject: [PATCH] v1.1.5 --- CC.Yi.API/Controllers/StudentController.cs | 5 +-- CC.Yi.API/Filter/DbContextFilter.cs | 22 ++++++++++++ CC.Yi.API/Startup.cs | 26 ++++++++++---- CC.Yi.DAL/DbContentFactory.cs | 41 ++++++++++++---------- CC.Yi.DALFactory/DbSession.cs | 2 +- 5 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 CC.Yi.API/Filter/DbContextFilter.cs diff --git a/CC.Yi.API/Controllers/StudentController.cs b/CC.Yi.API/Controllers/StudentController.cs index f0101f08..b3301a7e 100644 --- a/CC.Yi.API/Controllers/StudentController.cs +++ b/CC.Yi.API/Controllers/StudentController.cs @@ -1,4 +1,5 @@ -using CC.Yi.Common; +using CC.Yi.API.Filter; +using CC.Yi.Common; using CC.Yi.Common.Cache; using CC.Yi.IBLL; using CC.Yi.Model; @@ -15,7 +16,7 @@ namespace CC.Yi.API.Controllers { [ApiController] [Route("[controller]/[action]")] - public class StudentController : ControllerBase + public class StudentController : Controller { private readonly ILogger _logger;//处理日志相关文件 diff --git a/CC.Yi.API/Filter/DbContextFilter.cs b/CC.Yi.API/Filter/DbContextFilter.cs new file mode 100644 index 00000000..82fd8aac --- /dev/null +++ b/CC.Yi.API/Filter/DbContextFilter.cs @@ -0,0 +1,22 @@ +using CC.Yi.DAL; +using CC.Yi.Model; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace CC.Yi.API.Filter +{ + public class DbContextFilter : ActionFilterAttribute + { + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + var cache = filterContext.HttpContext.RequestServices.GetService(typeof(DataContext)) as DataContext; + DbContentFactory.Initialize(cache); + base.OnActionExecuting(filterContext); + + } + } +} diff --git a/CC.Yi.API/Startup.cs b/CC.Yi.API/Startup.cs index abe98ff5..1e429dd7 100644 --- a/CC.Yi.API/Startup.cs +++ b/CC.Yi.API/Startup.cs @@ -1,6 +1,7 @@ using Autofac; using Autofac.Extras.DynamicProxy; +using CC.Yi.API.Filter; using CC.Yi.BLL; using CC.Yi.Common.Cache; using CC.Yi.Common.Castle; @@ -35,7 +36,7 @@ namespace CC.Yi.API public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) { @@ -45,7 +46,16 @@ namespace CC.Yi.API c.SwaggerDoc("v1", new OpenApiInfo { Title = "CC.Yi.API", Version = "v1" }); }); services.AddSession(); - services.AddMvc(); + + + + //ù + Action filters = new Action(r => { + r.Filters.Add(typeof(DbContextFilter)); + }); + services.AddMvc(filters); + + //ݿ string connection1 = Configuration["ConnectionStringBySQL"]; string connection2 = Configuration["ConnectionStringByMySQL"]; string connection3 = Configuration["ConnectionStringBySQLite"]; @@ -78,7 +88,9 @@ namespace CC.Yi.API // //options.User.RequireUniqueEmail = false;//עǷԲظ // //options.User.AllowedUserNameCharacters="abcd"//ַֻ //}).AddEntityFrameworkStores().AddDefaultTokenProviders(); - services.AddCors(options => options.AddPolicy("CorsPolicy",// + + // + services.AddCors(options => options.AddPolicy("CorsPolicy", builder => { builder.AllowAnyMethod() @@ -88,17 +100,17 @@ namespace CC.Yi.API })); } + //ʼʹú private void InitData(IServiceProvider serviceProvider) { - var serviceScope = serviceProvider.GetRequiredService().CreateScope(); + //var serviceScope = serviceProvider.GetRequiredService().CreateScope(); - var context = serviceScope.ServiceProvider.GetService(); - DbContentFactory.Initialize(context);//þ̬෽ע + //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) { diff --git a/CC.Yi.DAL/DbContentFactory.cs b/CC.Yi.DAL/DbContentFactory.cs index 7e110070..73113740 100644 --- a/CC.Yi.DAL/DbContentFactory.cs +++ b/CC.Yi.DAL/DbContentFactory.cs @@ -9,34 +9,39 @@ namespace CC.Yi.DAL public class DbContentFactory { private static DataContext Webcontext; - + private static object myLock = new object(); public static void Initialize(DataContext webContext) { - Webcontext = webContext; + + Monitor.Enter(myLock); + { + Webcontext = webContext; + } } public static DataContext GetCurrentDbContent() { - object myLock = new object(); - lock (myLock) - { - //return new DataModelContainer(); - //一次请求共用一个上下文实例 - //每一次http请求都会开启一个新的线程,保证在一个线程中,DbContext是唯一的 - //CallContext:是线程内部唯一的数据槽(一块内存空间/容器),相当于一个键值对数据容器,通过key获取value了,需要引入System.Runtime.Remoting.Messaging;命名空间 - DataContext db = CallContext.GetData("DbContext") as DataContext; - //从 CallContext 中检索具有指定key“DbContext”的对象,并强转为DbContext + + DataContext db = CallContext.GetData("DbContext") as DataContext; if (db == null)//线程在数据槽里面没有此上下文 { - - - db = Webcontext; - CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value - } - - return db; + db = Webcontext; + CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value } + + try + { + Monitor.Exit(myLock); + } + catch + { + } + + + return db; + + } private static class CallContext diff --git a/CC.Yi.DALFactory/DbSession.cs b/CC.Yi.DALFactory/DbSession.cs index 7fa154f4..b6de042d 100644 --- a/CC.Yi.DALFactory/DbSession.cs +++ b/CC.Yi.DALFactory/DbSession.cs @@ -18,7 +18,7 @@ namespace CC.Yi.DALFactory try { // Attempt to save changes to the database - context.SaveChangesAsync(); + context.SaveChanges(); saved = true; } catch (DbUpdateConcurrencyException ex)