diff --git a/common/ASC.Core.Common/Data/DbSubscriptionService.cs b/common/ASC.Core.Common/Data/DbSubscriptionService.cs index f3db840da96..1420c3a2dfc 100644 --- a/common/ASC.Core.Common/Data/DbSubscriptionService.cs +++ b/common/ASC.Core.Common/Data/DbSubscriptionService.cs @@ -40,10 +40,12 @@ class DbSubscriptionService : ISubscriptionService { private Expression> FromSubscriptionToSubscriptionRecord { get; set; } private Expression> FromDbSubscriptionMethodToSubscriptionMethod { get; set; } - private UserDbContext UserDbContext { get; set; } + private Lazy LazyUserDbContext { get; } + private UserDbContext UserDbContext { get => LazyUserDbContext.Value; } + public DbSubscriptionService(DbContextManager dbContextManager) { - UserDbContext = dbContextManager.Value; + LazyUserDbContext = new Lazy(() => dbContextManager.Value); FromSubscriptionToSubscriptionRecord = r => new SubscriptionRecord { diff --git a/common/ASC.Feed/Data/FeedAggregateDataProvider.cs b/common/ASC.Feed/Data/FeedAggregateDataProvider.cs index 3eebdc826b6..5336453e109 100644 --- a/common/ASC.Feed/Data/FeedAggregateDataProvider.cs +++ b/common/ASC.Feed/Data/FeedAggregateDataProvider.cs @@ -48,12 +48,13 @@ public class FeedAggregateDataProvider private AuthContext AuthContext { get; } private TenantManager TenantManager { get; } private TenantUtil TenantUtil { get; } - private FeedDbContext FeedDbContext { get; } + private Lazy LazyFeedDbContext { get; } + private FeedDbContext FeedDbContext { get => LazyFeedDbContext.Value; } public FeedAggregateDataProvider(AuthContext authContext, TenantManager tenantManager, TenantUtil tenantUtil, DbContextManager dbContextManager) : this(authContext, tenantManager, tenantUtil) { - FeedDbContext = dbContextManager.Get(Constants.FeedDbId); + LazyFeedDbContext = new Lazy(() => dbContextManager.Get(Constants.FeedDbId)); } public FeedAggregateDataProvider(AuthContext authContext, TenantManager tenantManager, TenantUtil tenantUtil) diff --git a/common/ASC.Feed/Data/FeedReadedDataProvider.cs b/common/ASC.Feed/Data/FeedReadedDataProvider.cs index 59ccc9c42f8..ec63505c481 100644 --- a/common/ASC.Feed/Data/FeedReadedDataProvider.cs +++ b/common/ASC.Feed/Data/FeedReadedDataProvider.cs @@ -41,13 +41,14 @@ public class FeedReadedDataProvider private AuthContext AuthContext { get; } private TenantManager TenantManager { get; } - private FeedDbContext FeedDbContext { get; } + private Lazy LazyFeedDbContext { get; } + private FeedDbContext FeedDbContext { get => LazyFeedDbContext.Value; } public FeedReadedDataProvider(AuthContext authContext, TenantManager tenantManager, DbContextManager dbContextManager) { AuthContext = authContext; TenantManager = tenantManager; - FeedDbContext = dbContextManager.Get(dbId); + LazyFeedDbContext = new Lazy(() => dbContextManager.Get(dbId)); } public DateTime GetTimeReaded() diff --git a/common/ASC.IPSecurity/IPRestrictionsRepository.cs b/common/ASC.IPSecurity/IPRestrictionsRepository.cs index a8b62155b39..419aafcffa3 100644 --- a/common/ASC.IPSecurity/IPRestrictionsRepository.cs +++ b/common/ASC.IPSecurity/IPRestrictionsRepository.cs @@ -24,6 +24,7 @@ */ +using System; using System.Collections.Generic; using System.Linq; @@ -39,11 +40,12 @@ public class IPRestrictionsRepository { private const string dbId = "core"; - private TenantDbContext TenantDbContext { get; } + private Lazy LazyTenantDbContext { get; } + private TenantDbContext TenantDbContext { get => LazyTenantDbContext.Value; } public IPRestrictionsRepository(DbContextManager dbContextManager) { - TenantDbContext = dbContextManager.Get(dbId); + LazyTenantDbContext = new Lazy(() => dbContextManager.Get(dbId)); } public List Get(int tenant) diff --git a/common/ASC.VoipService/Dao/AbstractDao.cs b/common/ASC.VoipService/Dao/AbstractDao.cs index 17c2d1cd950..61b3602ff4c 100644 --- a/common/ASC.VoipService/Dao/AbstractDao.cs +++ b/common/ASC.VoipService/Dao/AbstractDao.cs @@ -36,11 +36,12 @@ public class AbstractDao { private readonly string dbid = "default"; - protected VoipDbContext VoipDbContext { get; set; } + private Lazy LazyVoipDbContext { get; } + protected VoipDbContext VoipDbContext { get => LazyVoipDbContext.Value; } protected AbstractDao(DbContextManager dbOptions, TenantManager tenantManager) { - VoipDbContext = dbOptions.Get(dbid); + LazyVoipDbContext = new Lazy(() => dbOptions.Get(dbid)); TenantID = tenantManager.GetCurrentTenant().TenantId; } diff --git a/common/services/ASC.AuditTrail/AuditEventsRepository.cs b/common/services/ASC.AuditTrail/AuditEventsRepository.cs index 217862bd108..35bfde54c25 100644 --- a/common/services/ASC.AuditTrail/AuditEventsRepository.cs +++ b/common/services/ASC.AuditTrail/AuditEventsRepository.cs @@ -44,14 +44,15 @@ public class AuditEventsRepository { private MessageTarget MessageTarget { get; set; } private UserFormatter UserFormatter { get; set; } - private AuditTrailContext AuditTrailContext { get; } + private Lazy LazyAuditTrailContext { get; } + private AuditTrailContext AuditTrailContext { get => LazyAuditTrailContext.Value; } private AuditActionMapper AuditActionMapper { get; } public AuditEventsRepository(MessageTarget messageTarget, UserFormatter userFormatter, DbContextManager dbContextManager, AuditActionMapper auditActionMapper) { MessageTarget = messageTarget; UserFormatter = userFormatter; - AuditTrailContext = dbContextManager.Value; + LazyAuditTrailContext = new Lazy(() => dbContextManager.Value ); AuditActionMapper = auditActionMapper; } diff --git a/common/services/ASC.AuditTrail/LoginEventsRepository.cs b/common/services/ASC.AuditTrail/LoginEventsRepository.cs index 34ef9f12fe4..2027ae0e177 100644 --- a/common/services/ASC.AuditTrail/LoginEventsRepository.cs +++ b/common/services/ASC.AuditTrail/LoginEventsRepository.cs @@ -43,14 +43,15 @@ namespace ASC.AuditTrail.Data [Scope] public class LoginEventsRepository { - private UserFormatter UserFormatter { get; } - private AuditTrailContext AuditTrailContext { get; } + private UserFormatter UserFormatter { get; } + private Lazy LazyAuditTrailContext { get; } + private AuditTrailContext AuditTrailContext { get => LazyAuditTrailContext.Value; } private AuditActionMapper AuditActionMapper { get; } public LoginEventsRepository(UserFormatter userFormatter, DbContextManager dbContextManager, AuditActionMapper auditActionMapper) { UserFormatter = userFormatter; - AuditTrailContext = dbContextManager.Value; + LazyAuditTrailContext = new Lazy(() => dbContextManager.Value); AuditActionMapper = auditActionMapper; } diff --git a/common/services/ASC.Data.Backup/Storage/BackupRepository.cs b/common/services/ASC.Data.Backup/Storage/BackupRepository.cs index eaa5c51e4f9..89dad1e9693 100644 --- a/common/services/ASC.Data.Backup/Storage/BackupRepository.cs +++ b/common/services/ASC.Data.Backup/Storage/BackupRepository.cs @@ -37,12 +37,13 @@ namespace ASC.Data.Backup.Storage { [Scope] public class BackupRepository : IBackupRepository - { - private BackupsContext BackupContext { get; } + { + private Lazy LazyBackupsContext { get; } + private BackupsContext BackupContext { get => LazyBackupsContext.Value; } public BackupRepository(DbContextManager backupContext) { - BackupContext = backupContext.Value; + LazyBackupsContext = new Lazy(() => backupContext.Value); } public void SaveBackupRecord(BackupRecord backup) diff --git a/common/services/ASC.Data.Backup/Tasks/BackupPortalTask.cs b/common/services/ASC.Data.Backup/Tasks/BackupPortalTask.cs index b2677d6ca1b..b3758e9e1df 100644 --- a/common/services/ASC.Data.Backup/Tasks/BackupPortalTask.cs +++ b/common/services/ASC.Data.Backup/Tasks/BackupPortalTask.cs @@ -63,7 +63,8 @@ public class BackupPortalTask : PortalTaskBase private bool Dump { get; set; } private TenantManager TenantManager { get; set; } private TempStream TempStream { get; } - private BackupsContext BackupRecordContext { get; set; } + private Lazy LazyBackupsContext { get; } + private BackupsContext BackupRecordContext { get => LazyBackupsContext.Value; } public BackupPortalTask(DbFactory dbFactory, DbContextManager dbContextManager, IOptionsMonitor options, TenantManager tenantManager, CoreBaseSettings coreBaseSettings, StorageFactory storageFactory, StorageFactoryConfig storageFactoryConfig, ModuleProvider moduleProvider, TempStream tempStream) : base(dbFactory, options, storageFactory, storageFactoryConfig, moduleProvider) @@ -71,7 +72,7 @@ public BackupPortalTask(DbFactory dbFactory, DbContextManager db Dump = coreBaseSettings.Standalone; TenantManager = tenantManager; TempStream = tempStream; - BackupRecordContext = dbContextManager.Get(DbFactory.ConnectionStringSettings.ConnectionString); + LazyBackupsContext = new Lazy(() => dbContextManager.Get(DbFactory.ConnectionStringSettings.ConnectionString)); } public void Init(int tenantId, string fromConfigPath, string toFilePath, int limit) diff --git a/common/services/ASC.ElasticSearch/Engine/BaseIndexer.cs b/common/services/ASC.ElasticSearch/Engine/BaseIndexer.cs index b34f656768a..2cff58569f4 100644 --- a/common/services/ASC.ElasticSearch/Engine/BaseIndexer.cs +++ b/common/services/ASC.ElasticSearch/Engine/BaseIndexer.cs @@ -92,8 +92,9 @@ public class BaseIndexer where T : class, ISearchItem private TenantManager TenantManager { get; } private BaseIndexerHelper BaseIndexerHelper { get; } private Settings Settings { get; } - private IServiceProvider ServiceProvider { get; } - private WebstudioDbContext WebstudioDbContext { get; } + private IServiceProvider ServiceProvider { get; } + private Lazy LazyWebstudioDbContext { get; } + private WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; } public BaseIndexer( Client client, @@ -110,7 +111,7 @@ public BaseIndexer( BaseIndexerHelper = baseIndexerHelper; Settings = settings; ServiceProvider = serviceProvider; - WebstudioDbContext = dbContextManager.Value; + LazyWebstudioDbContext = new Lazy(() => dbContextManager.Value); } internal void Index(T data, bool immediately = true) diff --git a/products/ASC.CRM/Server/Configuration/CRMsSpaceUsageStatManager.cs b/products/ASC.CRM/Server/Configuration/CRMsSpaceUsageStatManager.cs index 08ed2f1c381..b5beaf6ed0d 100644 --- a/products/ASC.CRM/Server/Configuration/CRMsSpaceUsageStatManager.cs +++ b/products/ASC.CRM/Server/Configuration/CRMsSpaceUsageStatManager.cs @@ -31,7 +31,7 @@ using ASC.Common; using ASC.Common.Web; using ASC.Core; -using ASC.Core.Common.EF; +using ASC.Core.Common.EF; using ASC.CRM.Resources; using ASC.Files.Core.EF; using ASC.Web.Core; @@ -44,8 +44,10 @@ namespace ASC.Web.CRM.Configuration [Scope] public class CrmSpaceUsageStatManager : SpaceUsageStatManager { - private int _tenantId; - private FilesDbContext _filesDbContext; + private int _tenantId; + private Lazy LazyFilesDbContext { get; } + private FilesDbContext _filesDbContext { get => LazyFilesDbContext.Value; } + private PathProvider _pathProvider; public CrmSpaceUsageStatManager(DbContextManager filesDbContext, @@ -53,7 +55,7 @@ public CrmSpaceUsageStatManager(DbContextManager filesDbContext, TenantManager tenantManager) { _pathProvider = pathProvider; - _filesDbContext = filesDbContext.Value; + LazyFilesDbContext = new Lazy(() => filesDbContext.Value); _tenantId = tenantManager.GetCurrentTenant().TenantId; } diff --git a/products/ASC.CRM/Server/Core/Dao/AbstractDao.cs b/products/ASC.CRM/Server/Core/Dao/AbstractDao.cs index 6f5e1e0b5a9..c81ddc715cc 100644 --- a/products/ASC.CRM/Server/Core/Dao/AbstractDao.cs +++ b/products/ASC.CRM/Server/Core/Dao/AbstractDao.cs @@ -45,8 +45,11 @@ namespace ASC.CRM.Core.Dao { public class AbstractDao { - protected readonly List _supportedEntityType = new List(); - public CrmDbContext CrmDbContext { get; } + protected readonly List _supportedEntityType = new List(); + + private Lazy LazyCrmDbContext { get; } + public CrmDbContext CrmDbContext { get => LazyCrmDbContext.Value; } + protected readonly SecurityContext _securityContext; protected readonly ICache _cache; protected ILog _logger; @@ -66,7 +69,7 @@ IMapper mapper _cache = ascCache; - CrmDbContext = dbContextManager.Get(CrmConstants.DatabaseId); + LazyCrmDbContext = new Lazy(() => dbContextManager.Get(CrmConstants.DatabaseId)); TenantID = tenantManager.GetCurrentTenant().TenantId; _securityContext = securityContext; diff --git a/products/ASC.CRM/Server/Core/Dao/ReportDao.cs b/products/ASC.CRM/Server/Core/Dao/ReportDao.cs index e6c6b9e8c49..30c1efe9556 100644 --- a/products/ASC.CRM/Server/Core/Dao/ReportDao.cs +++ b/products/ASC.CRM/Server/Core/Dao/ReportDao.cs @@ -73,13 +73,11 @@ public class ReportDao : AbstractDao private CurrencyInfo _defaultCurrency; private TenantUtil _tenantUtil; private DaoFactory _daoFactory; - private UserDbContext _userDbContext; private DisplayUserSettingsHelper _displayUserSettings; #region Constructor public ReportDao(DbContextManager dbContextManager, - DbContextManager dbUserContextManager, TenantManager tenantManager, SecurityContext securityContext, FilesIntegration filesIntegration, @@ -112,8 +110,6 @@ public ReportDao(DbContextManager dbContextManager, var crmSettings = settingsManager.Load(); - _userDbContext = dbUserContextManager.Get(CrmConstants.DatabaseId); - _defaultCurrency = currencyProvider.Get(crmSettings.DefaultCurrency); _displayUserSettings = displayUserSettingsHelper; @@ -313,7 +309,7 @@ public List GetMissingRates(string defaultCurrency) var file = _daoFactory.GetFileDao().SaveFile(document, stream); - SaveFile((int)file.ID, -1); + SaveFile(file.ID, -1); result.Add(file); } diff --git a/products/ASC.CRM/Server/Core/Dao/TaskDao.cs b/products/ASC.CRM/Server/Core/Dao/TaskDao.cs index 2aaeabbc983..f156ee121b6 100644 --- a/products/ASC.CRM/Server/Core/Dao/TaskDao.cs +++ b/products/ASC.CRM/Server/Core/Dao/TaskDao.cs @@ -52,10 +52,12 @@ namespace ASC.CRM.Core.Dao [Scope] public class TaskDao : AbstractDao { - private readonly UserDbContext _userDbContext; private readonly FactoryIndexerTask _factoryIndexer; private readonly TenantUtil _tenantUtil; private readonly CrmSecurity _crmSecurity; + + private Lazy LazyUserDbContext { get; } + private UserDbContext _userDbContext { get => LazyUserDbContext.Value; } public TaskDao(DbContextManager dbContextManager, TenantManager tenantManager, @@ -77,7 +79,7 @@ public TaskDao(DbContextManager dbContextManager, _crmSecurity = crmSecurity; _tenantUtil = tenantUtil; _factoryIndexer = factoryIndexer; - _userDbContext = userDbContext.Value; + LazyUserDbContext = new Lazy(() => userDbContext.Value); _mapper = mapper; } diff --git a/products/ASC.Files/Core/Configuration/FilesSpaceUsageStatManager.cs b/products/ASC.Files/Core/Configuration/FilesSpaceUsageStatManager.cs index 93ee2867bac..914f67276a3 100644 --- a/products/ASC.Files/Core/Configuration/FilesSpaceUsageStatManager.cs +++ b/products/ASC.Files/Core/Configuration/FilesSpaceUsageStatManager.cs @@ -44,8 +44,9 @@ namespace ASC.Web.Files { [Scope] public class FilesSpaceUsageStatManager : SpaceUsageStatManager - { - private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; } + { + private Lazy LazyFilesDbContext { get; } + private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } private TenantManager TenantManager { get; } private UserManager UserManager { get; } private UserPhotoManager UserPhotoManager { get; } @@ -64,7 +65,7 @@ public FilesSpaceUsageStatManager( GlobalFolderHelper globalFolderHelper, PathProvider pathProvider) { - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); TenantManager = tenantManager; UserManager = userManager; UserPhotoManager = userPhotoManager; @@ -126,15 +127,16 @@ public override List GetStatData() [Scope] public class FilesUserSpaceUsage : IUserSpaceUsage - { - private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; } + { + private Lazy LazyFilesDbContext { get; } + private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } private TenantManager TenantManager { get; } public FilesUserSpaceUsage( DbContextManager dbContextManager, TenantManager tenantManager) { - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); TenantManager = tenantManager; } diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs index e2d006a8f93..40c7026e61e 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs @@ -49,9 +49,10 @@ namespace ASC.Files.Core.Data { public class AbstractDao { - protected readonly ICache cache; - - public FilesDbContext FilesDbContext { get; } + protected readonly ICache cache; + + private Lazy LazyFilesDbContext { get; } + public FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } private int tenantID; protected internal int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); } @@ -83,7 +84,7 @@ protected AbstractDao( ICache cache) { this.cache = cache; - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); UserManager = userManager; TenantManager = tenantManager; TenantUtil = tenantUtil; diff --git a/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs b/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs index 28aeefd2664..c928ccd14dd 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs @@ -211,8 +211,9 @@ internal abstract class ThirdPartyProviderDao : ThirdPartyProviderDao, IDispo public int TenantID { get; private set; } protected IServiceProvider ServiceProvider { get; } protected UserManager UserManager { get; } - protected TenantUtil TenantUtil { get; } - protected FilesDbContext FilesDbContext { get; } + protected TenantUtil TenantUtil { get; } + private Lazy LazyFilesDbContext { get; } + protected FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } protected SetupInfo SetupInfo { get; } protected ILog Log { get; } protected FileUtility FileUtility { get; } @@ -237,7 +238,7 @@ public ThirdPartyProviderDao( ServiceProvider = serviceProvider; UserManager = userManager; TenantUtil = tenantUtil; - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); SetupInfo = setupInfo; Log = monitor.CurrentValue; FileUtility = fileUtility; diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs index c2423c2f26c..91015ab9aa3 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs @@ -80,8 +80,9 @@ public enum ProviderTypes internal class ProviderAccountDao : IProviderDao { private int tenantID; - protected int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); } - private FilesDbContext FilesDbContext { get; } + protected int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); } + private Lazy LazyFilesDbContext { get; } + private FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } public ILog Logger { get; } private IServiceProvider ServiceProvider { get; } private TenantUtil TenantUtil { get; } @@ -102,7 +103,7 @@ public ProviderAccountDao( DbContextManager dbContextManager, IOptionsMonitor options) { - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); Logger = options.Get("ASC.Files"); ServiceProvider = serviceProvider; TenantUtil = tenantUtil; diff --git a/products/ASC.Files/Core/ThirdPartyApp/Token.cs b/products/ASC.Files/Core/ThirdPartyApp/Token.cs index 6d4b91c9213..27421e5ec7a 100644 --- a/products/ASC.Files/Core/ThirdPartyApp/Token.cs +++ b/products/ASC.Files/Core/ThirdPartyApp/Token.cs @@ -88,8 +88,9 @@ public string GetRefreshedToken(TokenHelper tokenHelper) [Scope] public class TokenHelper { - public ILog Logger { get; } - private FilesDbContext FilesDbContext { get; } + public ILog Logger { get; } + private Lazy LazyFilesDbContext { get; } + private FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } private InstanceCrypto InstanceCrypto { get; } private AuthContext AuthContext { get; } private TenantManager TenantManager { get; } @@ -102,7 +103,7 @@ public TokenHelper( TenantManager tenantManager) { Logger = option.CurrentValue; - FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(FileConstant.DatabaseId)); InstanceCrypto = instanceCrypto; AuthContext = authContext; TenantManager = tenantManager; diff --git a/products/ASC.Files/Service/Thumbnail/FileDataProvider.cs b/products/ASC.Files/Service/Thumbnail/FileDataProvider.cs index 11f46b8d06f..2ecd520ff51 100644 --- a/products/ASC.Files/Service/Thumbnail/FileDataProvider.cs +++ b/products/ASC.Files/Service/Thumbnail/FileDataProvider.cs @@ -35,8 +35,10 @@ internal class FileDataProvider { private readonly ThumbnailSettings thumbnailSettings; private readonly ICache cache; - private readonly FilesDbContext filesDbContext; - private readonly CoreDbContext coreDbContext; + private Lazy LazyFilesDbContext { get; } + private FilesDbContext filesDbContext { get => LazyFilesDbContext.Value; } + private Lazy LazyCoreDbContext { get; } + private CoreDbContext coreDbContext { get => LazyCoreDbContext.Value; } private readonly string cacheKey; public FileDataProvider( @@ -48,8 +50,8 @@ DbContextManager coredbContextManager { this.thumbnailSettings = thumbnailSettings; cache = ascCache; - filesDbContext = dbContextManager.Get(thumbnailSettings.ConnectionStringName); - coreDbContext = coredbContextManager.Get(thumbnailSettings.ConnectionStringName); + LazyFilesDbContext = new Lazy(() => dbContextManager.Get(thumbnailSettings.ConnectionStringName)); + LazyCoreDbContext = new Lazy(() => coredbContextManager.Get(thumbnailSettings.ConnectionStringName)); cacheKey = "PremiumTenants"; } diff --git a/web/ASC.Web.Core/Files/FileUtility.cs b/web/ASC.Web.Core/Files/FileUtility.cs index decd7c12b1a..58849013b2e 100644 --- a/web/ASC.Web.Core/Files/FileUtility.cs +++ b/web/ASC.Web.Core/Files/FileUtility.cs @@ -41,7 +41,9 @@ namespace ASC.Web.Core.Files [Scope] public class FileUtility { - private DbContextManager FilesDbContext { get; set; } + private Lazy LazyFilesDbContext { get; } + private FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; } + public FileUtility( IConfiguration configuration, FilesLinkUtility filesLinkUtility, @@ -49,7 +51,7 @@ public FileUtility( { Configuration = configuration; FilesLinkUtility = filesLinkUtility; - FilesDbContext = dbContextManager; + LazyFilesDbContext = new Lazy(() => dbContextManager.Get("files")); CanForcesave = GetCanForcesave(); } @@ -178,9 +180,7 @@ public Dictionary> ExtsConvertible _extsConvertible = new Dictionary>(); if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceConverterUrl)) return _extsConvertible; - const string databaseId = "files"; - - var dbManager = FilesDbContext.Get(databaseId); + var dbManager = FilesDbContext; var list = dbManager.FilesConverts.Select(r => new { r.Input, r.Output }).ToList(); diff --git a/web/ASC.Web.Core/Mail/MailServiceHelper.cs b/web/ASC.Web.Core/Mail/MailServiceHelper.cs index fbf1b7f5ad7..4b5c8ff0261 100644 --- a/web/ASC.Web.Core/Mail/MailServiceHelper.cs +++ b/web/ASC.Web.Core/Mail/MailServiceHelper.cs @@ -78,7 +78,8 @@ public class MailServiceHelper private CoreBaseSettings CoreBaseSettings { get; } public MailServiceHelperStorage MailServiceHelperStorage { get; } private EFLoggerFactory LoggerFactory { get; } - private MailDbContext MailDbContext { get; } + private Lazy LazyMailDbContext { get; } + private MailDbContext MailDbContext { get => LazyMailDbContext.Value; } private ICache Cache { get; } public MailServiceHelper( @@ -97,7 +98,7 @@ public MailServiceHelper( CoreBaseSettings = coreBaseSettings; MailServiceHelperStorage = mailServiceHelperStorage; LoggerFactory = loggerFactory; - MailDbContext = dbContext.Get("webstudio"); + LazyMailDbContext = new Lazy(() => dbContext.Get("webstudio")); Cache = mailServiceHelperStorage.Cache; DefaultDatabase = GetDefaultDatabase(); } diff --git a/web/ASC.Web.Core/Mobile/MobileAppInstallRegistrator.cs b/web/ASC.Web.Core/Mobile/MobileAppInstallRegistrator.cs index f7f881f3f2c..6de24e13021 100644 --- a/web/ASC.Web.Core/Mobile/MobileAppInstallRegistrator.cs +++ b/web/ASC.Web.Core/Mobile/MobileAppInstallRegistrator.cs @@ -38,11 +38,12 @@ namespace ASC.Web.Core.Mobile [Scope] public class MobileAppInstallRegistrator : IMobileAppInstallRegistrator { - private DbContext DbContext { get; } + private Lazy LazyDbContext { get; } + private DbContext DbContext { get => LazyDbContext.Value; } public MobileAppInstallRegistrator(DbContextManager dbContext) { - DbContext = dbContext.Value; + LazyDbContext = new Lazy(() => dbContext.Value); } public void RegisterInstall(string userEmail, MobileAppType appType) diff --git a/web/ASC.Web.Core/Statistic/StatisticManager.cs b/web/ASC.Web.Core/Statistic/StatisticManager.cs index 83d12b6448d..7472be7d82b 100644 --- a/web/ASC.Web.Core/Statistic/StatisticManager.cs +++ b/web/ASC.Web.Core/Statistic/StatisticManager.cs @@ -45,11 +45,12 @@ public class StatisticManager private static readonly TimeSpan cacheTime = TimeSpan.FromMinutes(2); private static readonly IDictionary cache = new Dictionary(); - private WebstudioDbContext WebstudioDbContext { get; } + private Lazy LazyWebstudioDbContext { get; } + private WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; } public StatisticManager(DbContextManager dbContextManager) { - WebstudioDbContext = dbContextManager.Value; + LazyWebstudioDbContext = new Lazy(() => dbContextManager.Value); } public void SaveUserVisit(int tenantID, Guid userID, Guid productID)