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.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<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.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();
|
||||
|
||||
|
||||
|
||||
//<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 connection2 = Configuration["ConnectionStringByMySQL"];
|
||||
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.AllowedUserNameCharacters="abcd"//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
||||
//}).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.AllowAnyMethod()
|
||||
@@ -88,17 +100,17 @@ namespace CC.Yi.API
|
||||
}));
|
||||
}
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC>ʹ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
|
||||
private void InitData(IServiceProvider serviceProvider)
|
||||
{
|
||||
var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||
//var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||
|
||||
var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
||||
DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
||||
//var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
||||
//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)
|
||||
{
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user