Notify: IServiceProvider replaced by IServiceScopeFactory

This commit is contained in:
Maksim Chegulov 2022-02-16 17:41:00 +03:00
parent a457034941
commit 8503dc07cb
3 changed files with 18 additions and 19 deletions

View File

@ -31,19 +31,19 @@ public class DbWorker
private readonly string _dbid;
private readonly object _syncRoot = new object();
private readonly IServiceProvider _serviceProvider;
private readonly IServiceScopeFactory _serviceScopeFactory;
public NotifyServiceCfg NotifyServiceCfg { get; }
public DbWorker(IServiceProvider serviceProvider, IOptions<NotifyServiceCfg> notifyServiceCfg)
public DbWorker(IServiceScopeFactory serviceScopeFactory, IOptions<NotifyServiceCfg> notifyServiceCfg)
{
_serviceProvider = serviceProvider;
_serviceScopeFactory = serviceScopeFactory;
NotifyServiceCfg = notifyServiceCfg.Value;
_dbid = NotifyServiceCfg.ConnectionStringName;
}
public int SaveMessage(NotifyMessage m)
{
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<DbContextManager<NotifyDbContext>>().Get(_dbid);
using var tx = dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
@ -89,7 +89,7 @@ public class DbWorker
{
lock (_syncRoot)
{
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<DbContextManager<NotifyDbContext>>().Get(_dbid);
using var tx = dbContext.Database.BeginTransaction();
@ -144,10 +144,9 @@ public class DbWorker
}
}
public void ResetStates()
{
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<DbContextManager<NotifyDbContext>>().Get(_dbid);
var tr = dbContext.Database.BeginTransaction();
@ -164,7 +163,7 @@ public class DbWorker
public void SetState(int id, MailSendingState result)
{
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<DbContextManager<NotifyDbContext>>().Get(_dbid);
using var tx = dbContext.Database.BeginTransaction();

View File

@ -31,14 +31,14 @@ public class NotifyCleaner : IDisposable
private readonly ILog _logger;
private readonly ManualResetEvent _stop = new ManualResetEvent(false);
public NotifyServiceCfg NotifyServiceCfg { get; }
private readonly IServiceProvider _serviceProvider;
private readonly IServiceScopeFactory _serviceScopeFactory;
public readonly CancellationTokenSource _cancellationTokenSource;
public NotifyCleaner(IOptions<NotifyServiceCfg> notifyServiceCfg, IServiceProvider serviceProvider, IOptionsMonitor<ILog> options)
public NotifyCleaner(IOptions<NotifyServiceCfg> notifyServiceCfg, IServiceScopeFactory serviceScopeFactory, IOptionsMonitor<ILog> options)
{
_logger = options.Get("ASC.Notify");
NotifyServiceCfg = notifyServiceCfg.Value;
_serviceProvider = serviceProvider;
_serviceScopeFactory = serviceScopeFactory;
_cancellationTokenSource = new CancellationTokenSource();
}
@ -62,7 +62,7 @@ public class NotifyCleaner : IDisposable
{
var date = DateTime.UtcNow.AddDays(-NotifyServiceCfg.StoreMessagesDays);
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<DbContextManager<NotifyDbContext>>().Get(NotifyServiceCfg.ConnectionStringName);
using var tx = dbContext.Database.BeginTransaction();

View File

@ -32,12 +32,12 @@ public class NotifyService : INotifyService, IDisposable
private readonly ICacheNotify<NotifyMessage> _cacheNotify;
private readonly ICacheNotify<NotifyInvoke> _cacheInvoke;
private readonly DbWorker _db;
private readonly IServiceProvider _serviceProvider;
private readonly IServiceScopeFactory _serviceScopeFactory;
public NotifyService(DbWorker db, IServiceProvider serviceProvider, ICacheNotify<NotifyMessage> cacheNotify, ICacheNotify<NotifyInvoke> cacheInvoke, IOptionsMonitor<ILog> options)
public NotifyService(DbWorker db, IServiceScopeFactory serviceScopeFactory, ICacheNotify<NotifyMessage> cacheNotify, ICacheNotify<NotifyInvoke> cacheInvoke, IOptionsMonitor<ILog> options)
{
_db = db;
_serviceProvider = serviceProvider;
_serviceScopeFactory = serviceScopeFactory;
_cacheNotify = cacheNotify;
_cacheInvoke = cacheInvoke;
_logger = options.CurrentValue;
@ -45,13 +45,13 @@ public class NotifyService : INotifyService, IDisposable
public void Start()
{
_cacheNotify.Subscribe((n) => SendNotifyMessage(n), Common.Caching.CacheNotifyAction.InsertOrUpdate);
_cacheInvoke.Subscribe((n) => InvokeSendMethod(n), Common.Caching.CacheNotifyAction.InsertOrUpdate);
_cacheNotify.Subscribe((n) => SendNotifyMessage(n), CacheNotifyAction.InsertOrUpdate);
_cacheInvoke.Subscribe((n) => InvokeSendMethod(n), CacheNotifyAction.InsertOrUpdate);
}
public void Stop()
{
_cacheNotify.Unsubscribe(Common.Caching.CacheNotifyAction.InsertOrUpdate);
_cacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
}
public void SendNotifyMessage(NotifyMessage notifyMessage)
@ -75,7 +75,7 @@ public class NotifyService : INotifyService, IDisposable
var serviceType = Type.GetType(service, true);
using var scope = _serviceProvider.CreateScope();
using var scope = _serviceScopeFactory.CreateScope();
var instance = scope.ServiceProvider.GetService(serviceType);
if (instance == null)