MessaginSystem: IServiceProvider replaced by IServiceScopeFactory

This commit is contained in:
Maksim Chegulov 2022-02-16 11:49:19 +03:00
parent 26fdc40f84
commit fbd01bc585

View File

@ -40,16 +40,16 @@ public class MessagesRepository : IDisposable
private bool _timerStarted; private bool _timerStarted;
public ILog Logger { get; set; } public ILog Logger { get; set; }
private readonly IServiceProvider _serviceProvider; private readonly IServiceScopeFactory _serviceScopeFactory;
public MessagesRepository(IServiceProvider serviceProvider, IOptionsMonitor<ILog> options) public MessagesRepository(IServiceScopeFactory serviceScopeFactory, IOptionsMonitor<ILog> options)
{ {
_cacheTime = TimeSpan.FromMinutes(1); _cacheTime = TimeSpan.FromMinutes(1);
_cache = new Dictionary<string, EventMessage>(); _cache = new Dictionary<string, EventMessage>();
_timerStarted = false; _timerStarted = false;
Logger = options.CurrentValue; Logger = options.CurrentValue;
_serviceProvider = serviceProvider; _serviceScopeFactory = serviceScopeFactory;
_timer = new Timer(FlushCache); _timer = new Timer(FlushCache);
} }
@ -59,7 +59,7 @@ public class MessagesRepository : IDisposable
// messages with action code < 2000 are related to login-history // messages with action code < 2000 are related to login-history
if ((int)message.Action < 2000) if ((int)message.Action < 2000)
{ {
using var scope = _serviceProvider.CreateScope(); using var scope = _serviceScopeFactory.CreateScope();
using var ef = scope.ServiceProvider.GetService<DbContextManager<MessagesContext>>().Get("messages"); using var ef = scope.ServiceProvider.GetService<DbContextManager<MessagesContext>>().Get("messages");
AddLoginEvent(message, ef); AddLoginEvent(message, ef);
@ -105,7 +105,7 @@ public class MessagesRepository : IDisposable
return; return;
} }
using var scope = _serviceProvider.CreateScope(); using var scope = _serviceScopeFactory.CreateScope();
using var ef = scope.ServiceProvider.GetService<DbContextManager<Messages>>().Get("messages"); using var ef = scope.ServiceProvider.GetService<DbContextManager<Messages>>().Get("messages");
using var tx = ef.Database.BeginTransaction(IsolationLevel.ReadUncommitted); using var tx = ef.Database.BeginTransaction(IsolationLevel.ReadUncommitted);
var dict = new Dictionary<string, ClientInfo>(); var dict = new Dictionary<string, ClientInfo>();