该应用程序是在ASP NET Core 3上开发的。为了记录用户操作,我决定在Project类中使用单个方法。面临从不同线程使用一个单例dbContext的问题。
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string connection = Configuration.getconnectionString("ConnectionDB");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection),ServiceLifetime.Transient,ServiceLifetime.Singleton);
services.AddSingleton<Project>();
}
Project.cs
public async Task AddUserLog(string action,string message,int userId)
{
try
{
UserLog userLog = new UserLog()
{
action = action,Message = message,UserId = userId
Datepoint = DateTime.Now
};
_dbContext.UserLog.Add(userLog);
await _dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
await AddSystemLog("Project","AddUserLog",ex.Message);
}
}
SchemeController.cs
public class SchemeController : ControllerBase
{
private readonly Project _project;
public SchemeController(Project project)
{
_project = project;
}
[Authorize(Policy = "AdvancedControl")]
[HttpPost("[action]")]
public async Task SomeMethode()
{
for (int i = 0; i < 10; i++)
{
await _project.AddUserLog("Text","Message",42);
}
}
}
已经在循环的第二次迭代中,我在AddUserLog方法中捕获了一个异常: “ 在上一个操作完成之前,第二个操作是在此上下文上开始的。这通常是由使用相同DbContext实例的不同线程引起的。”
我提出了几种解决方案:
-
将日志添加到缓冲区表,然后通过计时器将其保存到数据库。但这不是最好的出路;
-
将方法保存到数据库时对其进行阻止。
但是我不喜欢任何选择。
请告诉我解决此问题的正确方法。