v1.1.5
This commit is contained in:
@@ -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.Common.Cache;
|
||||||
using CC.Yi.IBLL;
|
using CC.Yi.IBLL;
|
||||||
using CC.Yi.Model;
|
using CC.Yi.Model;
|
||||||
@@ -15,7 +16,7 @@ namespace CC.Yi.API.Controllers
|
|||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]/[action]")]
|
[Route("[controller]/[action]")]
|
||||||
public class StudentController : ControllerBase
|
public class StudentController : Controller
|
||||||
{
|
{
|
||||||
private readonly ILogger<StudentController> _logger;//处理日志相关文件
|
private readonly ILogger<StudentController> _logger;//处理日志相关文件
|
||||||
|
|
||||||
|
|||||||
22
CC.Yi.API/Filter/DbContextFilter.cs
Normal file
22
CC.Yi.API/Filter/DbContextFilter.cs
Normal file
@@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Extras.DynamicProxy;
|
using Autofac.Extras.DynamicProxy;
|
||||||
|
using CC.Yi.API.Filter;
|
||||||
using CC.Yi.BLL;
|
using CC.Yi.BLL;
|
||||||
using CC.Yi.Common.Cache;
|
using CC.Yi.Common.Cache;
|
||||||
using CC.Yi.Common.Castle;
|
using CC.Yi.Common.Castle;
|
||||||
@@ -35,7 +36,7 @@ namespace CC.Yi.API
|
|||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
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)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -45,7 +46,16 @@ namespace CC.Yi.API
|
|||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CC.Yi.API", Version = "v1" });
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CC.Yi.API", Version = "v1" });
|
||||||
});
|
});
|
||||||
services.AddSession();
|
services.AddSession();
|
||||||
services.AddMvc();
|
|
||||||
|
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
Action<MvcOptions> filters = new Action<MvcOptions>(r => {
|
||||||
|
r.Filters.Add(typeof(DbContextFilter));
|
||||||
|
});
|
||||||
|
services.AddMvc(filters);
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||||
string connection1 = Configuration["ConnectionStringBySQL"];
|
string connection1 = Configuration["ConnectionStringBySQL"];
|
||||||
string connection2 = Configuration["ConnectionStringByMySQL"];
|
string connection2 = Configuration["ConnectionStringByMySQL"];
|
||||||
string connection3 = Configuration["ConnectionStringBySQLite"];
|
string connection3 = Configuration["ConnectionStringBySQLite"];
|
||||||
@@ -78,7 +88,9 @@ namespace CC.Yi.API
|
|||||||
// //options.User.RequireUniqueEmail = false;//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ظ<EFBFBD>
|
// //options.User.RequireUniqueEmail = false;//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ظ<EFBFBD>
|
||||||
// //options.User.AllowedUserNameCharacters="abcd"//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
// //options.User.AllowedUserNameCharacters="abcd"//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
||||||
//}).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
|
//}).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
|
||||||
services.AddCors(options => options.AddPolicy("CorsPolicy",//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
services.AddCors(options => options.AddPolicy("CorsPolicy",
|
||||||
builder =>
|
builder =>
|
||||||
{
|
{
|
||||||
builder.AllowAnyMethod()
|
builder.AllowAnyMethod()
|
||||||
@@ -88,17 +100,17 @@ namespace CC.Yi.API
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//<2F><>ʼ<EFBFBD><CABC>ʹ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
|
||||||
private void InitData(IServiceProvider serviceProvider)
|
private void InitData(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
//var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||||
|
|
||||||
var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
//var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
||||||
DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
//DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -9,34 +9,39 @@ namespace CC.Yi.DAL
|
|||||||
public class DbContentFactory
|
public class DbContentFactory
|
||||||
{
|
{
|
||||||
private static DataContext Webcontext;
|
private static DataContext Webcontext;
|
||||||
|
private static object myLock = new object();
|
||||||
|
|
||||||
public static void Initialize(DataContext webContext)
|
public static void Initialize(DataContext webContext)
|
||||||
{
|
{
|
||||||
Webcontext = webContext;
|
|
||||||
|
Monitor.Enter(myLock);
|
||||||
|
{
|
||||||
|
Webcontext = webContext;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static DataContext GetCurrentDbContent()
|
public static DataContext GetCurrentDbContent()
|
||||||
{
|
{
|
||||||
object myLock = new object();
|
|
||||||
lock (myLock)
|
DataContext db = CallContext.GetData("DbContext") as DataContext;
|
||||||
{
|
|
||||||
//return new DataModelContainer();
|
|
||||||
//一次请求共用一个上下文实例
|
|
||||||
//每一次http请求都会开启一个新的线程,保证在一个线程中,DbContext是唯一的
|
|
||||||
//CallContext:是线程内部唯一的数据槽(一块内存空间/容器),相当于一个键值对数据容器,通过key获取value了,需要引入System.Runtime.Remoting.Messaging;命名空间
|
|
||||||
DataContext db = CallContext.GetData("DbContext") as DataContext;
|
|
||||||
//从 CallContext 中检索具有指定key“DbContext”的对象,并强转为DbContext
|
|
||||||
|
|
||||||
if (db == null)//线程在数据槽里面没有此上下文
|
if (db == null)//线程在数据槽里面没有此上下文
|
||||||
{
|
{
|
||||||
|
db = Webcontext;
|
||||||
|
CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Monitor.Exit(myLock);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
db = Webcontext;
|
|
||||||
CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value
|
|
||||||
}
|
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CallContext
|
private static class CallContext
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CC.Yi.DALFactory
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Attempt to save changes to the database
|
// Attempt to save changes to the database
|
||||||
context.SaveChangesAsync();
|
context.SaveChanges();
|
||||||
saved = true;
|
saved = true;
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException ex)
|
catch (DbUpdateConcurrencyException ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user