diff --git a/BLAZAM/BLAZAM.csproj b/BLAZAM/BLAZAM.csproj index 67dc8d87..1f66b1b4 100644 --- a/BLAZAM/BLAZAM.csproj +++ b/BLAZAM/BLAZAM.csproj @@ -6,7 +6,7 @@ enable false 1.2.3 - 2024.12.19.2303 + 2024.12.22.1816 false BLAZAM True diff --git a/BLAZAM/Middleware/ApplicationStatusRedirectMiddleware.cs b/BLAZAM/Middleware/ApplicationStatusRedirectMiddleware.cs index 3a780979..706a0122 100644 --- a/BLAZAM/Middleware/ApplicationStatusRedirectMiddleware.cs +++ b/BLAZAM/Middleware/ApplicationStatusRedirectMiddleware.cs @@ -22,7 +22,7 @@ public ApplicationStatusRedirectMiddleware( _monitor = monitor; } - public async Task InvokeAsync(HttpContext context, IAppDatabaseFactory factory) + public async Task InvokeAsync(HttpContext context, IUserDatabaseFactory factory) { intendedUri = context.Request.Path.ToUriComponent(); if (!InIgnoreList(intendedUri)) diff --git a/BLAZAM/Pages/API/v1/ApiController.cs b/BLAZAM/Pages/API/v1/ApiController.cs index dadb2ba4..0eeb62ef 100644 --- a/BLAZAM/Pages/API/v1/ApiController.cs +++ b/BLAZAM/Pages/API/v1/ApiController.cs @@ -30,7 +30,7 @@ public class ApiController : Controller /// /// A factory for connections /// - protected readonly IAppDatabaseFactory DbFactory; + protected readonly IUserDatabaseFactory DbFactory; /// /// The API audit logger /// @@ -45,7 +45,7 @@ public class ApiController : Controller /// protected IApplicationUserState? CurrentUserState { get; } - public ApiController(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) + public ApiController(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) { //User = httpContextAccessor.HttpContext.User; AuditLogger = audit; diff --git a/BLAZAM/Pages/API/v1/Search.cs b/BLAZAM/Pages/API/v1/Search.cs index fec7e303..144a5d8a 100644 --- a/BLAZAM/Pages/API/v1/Search.cs +++ b/BLAZAM/Pages/API/v1/Search.cs @@ -13,7 +13,7 @@ namespace BLAZAM.Pages.API.v1 [Produces("application/json")] public class Search : ApiController { - public Search(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) + public Search(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) { } diff --git a/BLAZAM/Pages/API/v1/Templates.cs b/BLAZAM/Pages/API/v1/Templates.cs index e64f99f5..86c8e2a0 100644 --- a/BLAZAM/Pages/API/v1/Templates.cs +++ b/BLAZAM/Pages/API/v1/Templates.cs @@ -29,12 +29,12 @@ namespace BLAZAM.Pages.API.v1 /// public class Templates : ApiController { - private IAppDatabaseFactory _appDatabaseFactory; + private IUserDatabaseFactory _appDatabaseFactory; private IStringLocalizer AppLocalization; private EmailService EmailService; private NotificationGenerationService OUNotificationService; - public Templates(NotificationGenerationService ouNotificationService, EmailService email, IApplicationUserStateService applicationUserStateService, IStringLocalizer localizer, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) + public Templates(NotificationGenerationService ouNotificationService, EmailService email, IApplicationUserStateService applicationUserStateService, IStringLocalizer localizer, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) { AppLocalization = localizer; EmailService = email; diff --git a/BLAZAM/Pages/API/v1/Test.cs b/BLAZAM/Pages/API/v1/Test.cs index 6841ee53..ee8b2005 100644 --- a/BLAZAM/Pages/API/v1/Test.cs +++ b/BLAZAM/Pages/API/v1/Test.cs @@ -11,7 +11,7 @@ namespace BLAZAM.Pages.API.v1 { public class Test : ApiController { - public Test(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) + public Test(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory) { } diff --git a/BLAZAM/Pages/Benchmark.razor b/BLAZAM/Pages/Benchmark.razor index d45b984a..af8376ca 100644 --- a/BLAZAM/Pages/Benchmark.razor +++ b/BLAZAM/Pages/Benchmark.razor @@ -1,4 +1,4 @@ -@inject IAppDatabaseFactory factory +@inject IUserDatabaseFactory factory @inject IActiveDirectoryContextFactory directoryFactory @inherits AppComponentBase @page "/benchmark" diff --git a/BLAZAM/Pages/Error/UnhandledExceptionPage.razor b/BLAZAM/Pages/Error/UnhandledExceptionPage.razor index 3a5c3ece..097c46cd 100644 --- a/BLAZAM/Pages/Error/UnhandledExceptionPage.razor +++ b/BLAZAM/Pages/Error/UnhandledExceptionPage.razor @@ -1,6 +1,6 @@ @inject EmailService email @inject ApplicationInfo ApplicationInfo -@inject IAppDatabaseFactory factory +@inject IUserDatabaseFactory factory @inject NavigationManager Nav Error diff --git a/BLAZAM/Pages/PWAManifest.cshtml.cs b/BLAZAM/Pages/PWAManifest.cshtml.cs index dee5dcfa..e8a21cb6 100644 --- a/BLAZAM/Pages/PWAManifest.cshtml.cs +++ b/BLAZAM/Pages/PWAManifest.cshtml.cs @@ -27,9 +27,9 @@ public class PWAManifest [Produces("application/json")] public class PWAManifestModel : PageModel { - private readonly IAppDatabaseFactory _factory; + private readonly IUserDatabaseFactory _factory; - public PWAManifestModel(IAppDatabaseFactory factory) + public PWAManifestModel(IUserDatabaseFactory factory) { _factory = factory; } diff --git a/BLAZAM/Pages/Static.cshtml.cs b/BLAZAM/Pages/Static.cshtml.cs index bc321d50..0cab3fd9 100644 --- a/BLAZAM/Pages/Static.cshtml.cs +++ b/BLAZAM/Pages/Static.cshtml.cs @@ -21,7 +21,7 @@ public class StaticModel : PageModel protected IDatabaseContext Context { get; private set; } - public StaticModel(IAppDatabaseFactory factory) + public StaticModel(IUserDatabaseFactory factory) { Context = factory.CreateDbContext(); diff --git a/BLAZAM/Program.cs b/BLAZAM/Program.cs index 61685259..40c0bfe8 100644 --- a/BLAZAM/Program.cs +++ b/BLAZAM/Program.cs @@ -214,7 +214,7 @@ private static void SetupKestrel(WebApplicationBuilder builder) { var _programDbFactory = new AppDatabaseFactory(Configuration); - var kestrelContext = _programDbFactory.CreateDbContext(); + using var kestrelContext = _programDbFactory.CreateDbContext(); if (!ApplicationInfo.isUnderIIS && !Debugger.IsAttached) diff --git a/BLAZAM/ProgramHelpers.cs b/BLAZAM/ProgramHelpers.cs index 187b063d..60351ce9 100644 --- a/BLAZAM/ProgramHelpers.cs +++ b/BLAZAM/ProgramHelpers.cs @@ -227,6 +227,7 @@ public static WebApplicationBuilder InjectServices(this WebApplicationBuilder bu builder.Services.AddSingleton(); + builder.Services.AddScoped(); //Provide an Http client as a service with custom construction via api service class builder.Services.AddHttpClient(); @@ -407,7 +408,7 @@ public static void PreRun(this WebApplication application) //Setup Seq logging if allowed by admin try { - var context = Program.AppInstance.Services.GetRequiredService().CreateDbContext(); + using var context = Program.AppInstance.Services.GetRequiredService().CreateDbContext(); if (context != null && context.AppSettings.FirstOrDefault()?.SendLogsToDeveloper != null) { Loggers.SendToSeqServer = context.AppSettings.FirstOrDefault().SendLogsToDeveloper; diff --git a/BLAZAMCommon/Data/ApplicationStatistics.cs b/BLAZAMCommon/Data/ApplicationStatistics.cs new file mode 100644 index 00000000..051ba472 --- /dev/null +++ b/BLAZAMCommon/Data/ApplicationStatistics.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.ObjectPool; +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BLAZAM.Common.Data +{ + public static class ApplicationStatistics + { + public static int ADContextCount { get; private set; } + public static void AddADContext() + { + ADContextCount++; + } + public static void RemoveADContext() + { + if (ADContextCount > 0) + ADContextCount--; + + } + + + public static int DBContextCount { get; private set; } + public static void AddDBContext() + { + DBContextCount++; + } + public static void RemoveDBContext() + { + if (DBContextCount > 0) + DBContextCount--; + + } + } +} diff --git a/BLAZAMDatabase/Context/AppDatabaseFactory.cs b/BLAZAMDatabase/Context/AppDatabaseFactory.cs index 7a478b30..59cf9059 100644 --- a/BLAZAMDatabase/Context/AppDatabaseFactory.cs +++ b/BLAZAMDatabase/Context/AppDatabaseFactory.cs @@ -56,7 +56,7 @@ public AppDatabaseFactory(IConfiguration configuration) /// private void SeedData() { - var seedContext = this.CreateDbContext(); + using var seedContext = this.CreateDbContext(); SetupDenyAll(seedContext); @@ -177,7 +177,10 @@ private bool CheckInstallation() /// Async call to /// /// - public async Task CreateDbContextAsync() => await Task.Run(() => { return CreateDbContext(); }); + public async Task CreateDbContextAsync() + { + return await Task.Run(() => { return CreateDbContext(); }); + } public DatabaseType DatabaseType { get @@ -190,7 +193,7 @@ public DatabaseType DatabaseType { case "sql": - return DatabaseType.SQL; + return DatabaseType.SQL; case "sqlite": return DatabaseType.SQLite; @@ -217,7 +220,7 @@ public DatabaseType DatabaseType /// Thrown for unexpected exceptions public IDatabaseContext CreateDbContext() { - + IDatabaseContext? databaseContext = null; switch (DatabaseType) { diff --git a/BLAZAMDatabase/Context/DatabaseContextBase.cs b/BLAZAMDatabase/Context/DatabaseContextBase.cs index ce9fe165..9a4acebb 100644 --- a/BLAZAMDatabase/Context/DatabaseContextBase.cs +++ b/BLAZAMDatabase/Context/DatabaseContextBase.cs @@ -24,6 +24,13 @@ namespace BLAZAM.Database.Context public class DatabaseContextBase : DbContext, IDatabaseContext { + public override void Dispose() + { + ApplicationStatistics.RemoveDBContext(); + + base.Dispose(); + } + @@ -76,12 +83,15 @@ public enum DatabaseStatus public DatabaseContextBase() { ConnectionString = new(""); + ApplicationStatistics.AddDBContext(); + } public DatabaseContextBase(DatabaseConnectionString databaseConnectionString) : base() { ConnectionString = databaseConnectionString; + ApplicationStatistics.AddDBContext(); } @@ -93,6 +103,8 @@ public DatabaseContextBase(DatabaseConnectionString databaseConnectionString) : public DatabaseContextBase(DbContextOptions options) : base(options) { + ApplicationStatistics.AddDBContext(); + } //App Settings diff --git a/BLAZAMDatabase/Context/IUserDatabaseFactory.cs b/BLAZAMDatabase/Context/IUserDatabaseFactory.cs new file mode 100644 index 00000000..14349330 --- /dev/null +++ b/BLAZAMDatabase/Context/IUserDatabaseFactory.cs @@ -0,0 +1,14 @@ +using BLAZAM.Common.Data.Database; +using BLAZAM.Database.Exceptions; + +namespace BLAZAM.Database.Context +{ + /// + /// The primary database factory for BLAZAM. + /// Creates types + /// that can have a number of database type backings + /// + public interface IUserDatabaseFactory : IAppDatabaseFactory + { + } +} \ No newline at end of file diff --git a/BLAZAMDatabase/Context/UserDatabaseFactory.cs b/BLAZAMDatabase/Context/UserDatabaseFactory.cs new file mode 100644 index 00000000..2869c27b --- /dev/null +++ b/BLAZAMDatabase/Context/UserDatabaseFactory.cs @@ -0,0 +1,52 @@ +using BLAZAM.Common.Data.Database; +using System; +using System.Collections.Generic; +using System.Data.Entity.Core.Mapping; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BLAZAM.Database.Context +{ + public class UserDatabaseFactory : IUserDatabaseFactory, IDisposable + { + private readonly IAppDatabaseFactory _appDatabaseFactory; + + public UserDatabaseFactory(IAppDatabaseFactory appDatabaseFactory) + { + _appDatabaseFactory = appDatabaseFactory; + } + + public DatabaseType DatabaseType => _appDatabaseFactory.DatabaseType; + + public bool ApplyDatabaseMigrations(bool force = false) => _appDatabaseFactory.ApplyDatabaseMigrations(force); + + public Task ApplyDatabaseMigrationsAsync(bool force = false) => _appDatabaseFactory.ApplyDatabaseMigrationsAsync(force); + + private List _userContexts = new(); + + public IDatabaseContext CreateDbContext() + { + var ctx= _appDatabaseFactory.CreateDbContext(); + _userContexts.Add(ctx); + + return ctx; + } + + public async Task CreateDbContextAsync() + { + var ctx = await _appDatabaseFactory.CreateDbContextAsync(); + _userContexts.Add(ctx); + + return ctx; + } + + public void Dispose() + { + foreach(var ctx in _userContexts) + { + ctx.Dispose(); + } + } + } +} diff --git a/BLAZAMGui/Layouts/LoginLayout.razor b/BLAZAMGui/Layouts/LoginLayout.razor index 652a00d9..0a378e8d 100644 --- a/BLAZAMGui/Layouts/LoginLayout.razor +++ b/BLAZAMGui/Layouts/LoginLayout.razor @@ -1,7 +1,7 @@ @inherits LayoutComponentBase @inject IActiveDirectoryContext directory -@inject IAppDatabaseFactory DbFactory +@inject IUserDatabaseFactory DbFactory @inject ConnMonitor monitor diff --git a/BLAZAMGui/Layouts/MainLayout.razor b/BLAZAMGui/Layouts/MainLayout.razor index 168203d7..92ae6569 100644 --- a/BLAZAMGui/Layouts/MainLayout.razor +++ b/BLAZAMGui/Layouts/MainLayout.razor @@ -5,7 +5,7 @@ @inject NavigationManager Nav @inject IActiveDirectoryContext directory @inject IApplicationUserStateService UserStateService -@inject IAppDatabaseFactory DbFactory +@inject IUserDatabaseFactory DbFactory @inject AppSnackBarService SnackBarService @inject ApplicationInfo ApplicationInfo @inject ConnMonitor monitor diff --git a/BLAZAMGui/UI/AppComponentBase.cs b/BLAZAMGui/UI/AppComponentBase.cs index daa42dcf..084409bf 100644 --- a/BLAZAMGui/UI/AppComponentBase.cs +++ b/BLAZAMGui/UI/AppComponentBase.cs @@ -90,7 +90,7 @@ protected bool LoadingData } //protected IDatabaseContext? Context; [Inject] - protected IAppDatabaseFactory DbFactory { get; set; } + protected IUserDatabaseFactory DbFactory { get; set; } protected override void OnInitialized() diff --git a/BLAZAMGui/UI/DirectoryEntryViewHeader.razor b/BLAZAMGui/UI/DirectoryEntryViewHeader.razor index c40c4dc9..bb4926fb 100644 --- a/BLAZAMGui/UI/DirectoryEntryViewHeader.razor +++ b/BLAZAMGui/UI/DirectoryEntryViewHeader.razor @@ -41,7 +41,7 @@ else } @if (DirectoryEntry.OU!=null) { - @DirectoryEntry.OU.ToPrettyOu() + @DirectoryEntry.OU.ToPrettyOu() } @@ -52,25 +52,25 @@ else @if (accountEntry?.LockedOut == true) { + OnClick=Unlock + Icon=@Icons.Material.Filled.Lock /> } + Style="height:32px" + Gutters=false + Dense=true> @if (accountEntry != null) { + Icon="@Icons.Material.Filled.PersonOff" + Color="Color.Error" + Disabled=@(!(accountEntry.Enabled && accountEntry.CanDisable) && !(accountEntry.Disabled && accountEntry.CanEnable)) + ToggledIcon="@Icons.Material.Filled.Person" + ToggledColor="Color.Success" /> } @@ -79,11 +79,11 @@ else + ToggledChanged="@(async(state)=>{await ToggleEditMode(state);})" + Icon="@Icons.Material.Filled.Edit" + ToggledIcon="@Icons.Material.Filled.EditOff" + ToggledColor="Color.Error" + Disabled=@(!DirectoryEntry.CanEdit && (accountEntry!=null)) /> @@ -92,9 +92,9 @@ else + Icon="@Icons.Material.Filled.LockOpen" + Disabled=@(!accountEntry.CanUnlock) + aria-label="unlock" /> } @if (groupableEntry != null) @@ -102,8 +102,8 @@ else + Icon="@Icons.Material.Filled.GroupAdd" + aria-label="assign to" /> } @@ -111,34 +111,34 @@ else { + Disabled=@(!accountEntry.CanSetPassword) + Icon="@Icons.Material.Filled.Password" + aria-label="change password" /> } + Disabled=@(!DirectoryEntry.CanRename) + Icon="@Icons.Material.Filled.DriveFileRenameOutline" + aria-label="rename" /> + Disabled=@(!DirectoryEntry.CanMove) + Icon="@Icons.Material.Filled.DriveFileMove" + aria-label="move" /> + Color="Color.Error" + Disabled=@(!DirectoryEntry.CanDelete) + Icon="@Icons.Material.Filled.Delete" + aria-label="change password" /> @if (CurrentUser.State.IsSuperAdmin) @@ -146,26 +146,26 @@ else + Disabled=@(!DirectoryEntry.CanEdit) + Icon="@Icons.Material.Filled.History" /> + Icon="@Icons.Material.Filled.RunCircle" /> } @if (_showRequestButton) { + Icon="@Icons.Material.Filled.PlaylistAdd" /> } + Icon="@(IsFavorite==true?Icons.Material.Filled.Star:Icons.Material.TwoTone.Star)" + Color="@(IsFavorite==true?Color.Warning:Color.Default)" /> @@ -174,18 +174,18 @@ else + IconColor="@Color.Tertiary" + StartIcon="@Icons.Material.Filled.Settings"> @if (accountEntry != null) { @if ((accountEntry.Enabled && accountEntry.CanDisable) || (accountEntry.Disabled && accountEntry.CanEnable)) { + Color=Color.Success + Label=@AppLocalization["Account Enabled"] + @bind-Value=@accountEntry.Enabled + Disabled=@((accountEntry.Disabled && !accountEntry.CanEnable)||(accountEntry.Enabled && !accountEntry.CanDisable)) /> } @@ -238,6 +238,7 @@ else @code { private AppModal? _requestAccessModal; private bool _showRequestButton; + private IDirectoryEntryAdapter? _parentOU; [Parameter] public EventCallback OnDelete { get; set; } private async Task DeleteUser() @@ -329,11 +330,12 @@ else protected override async Task OnInitializedAsync() { - + await base.OnInitializedAsync(); LoadingData = true; _showRequestButton = Context.GlobalPermissionSettings.FirstOrDefault()?.AllowAccessRequest == true && Context.GlobalPermissionRequestActions.Count() > 0; + _parentOU =await DirectoryEntry.GetParentAsync(); } protected override void OnAfterRender(bool firstRender) { diff --git a/BLAZAMGui/UI/Settings/Notifications/NotificationSettings.razor b/BLAZAMGui/UI/Settings/Notifications/NotificationSettings.razor index d84eaefe..6a9c72f6 100644 --- a/BLAZAMGui/UI/Settings/Notifications/NotificationSettings.razor +++ b/BLAZAMGui/UI/Settings/Notifications/NotificationSettings.razor @@ -21,7 +21,7 @@ private async Task> SearchUsers(string value,CancellationToken token) { if (value == null) value = String.Empty; - return Users.Where(x => x.Username.Contains(value)); + return Users.Where(x => x.Username.Contains(value)).OrderBy(x=>x.Username); } diff --git a/BLAZAMGui/UI/Settings/ProcessResourceUsage.razor b/BLAZAMGui/UI/Settings/ProcessResourceUsage.razor index 361baf3a..af3e00a5 100644 --- a/BLAZAMGui/UI/Settings/ProcessResourceUsage.razor +++ b/BLAZAMGui/UI/Settings/ProcessResourceUsage.razor @@ -22,7 +22,13 @@ @AppLocalization["Loading"] } + + @ApplicationStatistics.ADContextCount + + + @ApplicationStatistics.DBContextCount + @code { [Parameter] public Process Process { get; set; } diff --git a/BLAZAMGui/UI/Settings/SystemSettings.razor b/BLAZAMGui/UI/Settings/SystemSettings.razor index 4616d1ad..dbcd0e6f 100644 --- a/BLAZAMGui/UI/Settings/SystemSettings.razor +++ b/BLAZAMGui/UI/Settings/SystemSettings.razor @@ -80,7 +80,7 @@ - + @AppLocalization["Jobs Monitor"] diff --git a/BLAZAMServices/AppAuthenticationStateProvider.cs b/BLAZAMServices/AppAuthenticationStateProvider.cs index ecad1c3d..6b411d90 100644 --- a/BLAZAMServices/AppAuthenticationStateProvider.cs +++ b/BLAZAMServices/AppAuthenticationStateProvider.cs @@ -24,7 +24,7 @@ namespace BLAZAM.Services /// public class AppAuthenticationStateProvider : AuthenticationStateProvider { - public AppAuthenticationStateProvider(IAppDatabaseFactory factory, + public AppAuthenticationStateProvider(IUserDatabaseFactory factory, IActiveDirectoryContext directoy, PermissionApplicator permissionHandler, IApplicationUserStateService userStateService, @@ -56,7 +56,7 @@ public AppAuthenticationStateProvider(IAppDatabaseFactory factory, private readonly IEncryptionService _encryption; private readonly NavigationManager _nav; private readonly IActiveDirectoryContext _directory; - private readonly IAppDatabaseFactory _factory; + private readonly IUserDatabaseFactory _factory; private readonly PermissionApplicator _permissionHandler; private readonly IApplicationUserStateService _userStateService; diff --git a/BLAZAMServices/Background/NotificationGenerationService.cs b/BLAZAMServices/Background/NotificationGenerationService.cs index 54f80130..a5d1015a 100644 --- a/BLAZAMServices/Background/NotificationGenerationService.cs +++ b/BLAZAMServices/Background/NotificationGenerationService.cs @@ -56,7 +56,8 @@ await Task.Run(async () => NotificationTemplateComponent? emailMessage; PackageNotification(source, notificationType, actor, target, out notification, out notificationTitle, out emailMessage); var _emailConfigured = _emailService.IsConfigured; - var users = Context.UserSettings.Include(us => us.NotificationSubscriptions).ToList(); + using var context = Context; + var users = context.UserSettings.Include(us => us.NotificationSubscriptions).ToList(); if (_databaseFactory.DatabaseType == DatabaseType.SQLite) { @@ -112,7 +113,8 @@ private async Task ProcessUserNotification(IDirectoryEntryAdapter source, Notifi private async Task PostWebHooks(IDirectoryEntryAdapter source, NotificationType notificationType, IApplicationUserState? actor = null, IDirectoryEntryAdapter? target = null) { - var webhooks = await Context.WebHookSubscriptions.Where(w => w.DeletedAt == null) + using var context = Context; + var webhooks = await context.WebHookSubscriptions.Where(w => w.DeletedAt == null) .Include(w => w.NotificationTypes) .Where(x => x.DeletedAt == null) .ToListAsync(); @@ -250,7 +252,7 @@ public NotificationSubscription CalculateEffectiveEmailSubscriptions(AppUser use ou = ou.GetParent(); if (ou is not IADOrganizationalUnit) return default; - var context = Context; + using var context = Context; NotificationSubscription effectiveByEmailSubscription = new(); effectiveByEmailSubscription = new(); @@ -309,7 +311,7 @@ public NotificationSubscription CalculateEffectiveInAppSubscriptions(AppUser use ou = ou.GetParent(); if (ou is not IADOrganizationalUnit) return default; - var context = Context; + using var context = Context; NotificationSubscription effectiveInAppSubscription = new(); effectiveInAppSubscription = new(); effectiveInAppSubscription.OU = ou.DN; diff --git a/BLAZAMSession/ApplicationUserStateService.cs b/BLAZAMSession/ApplicationUserStateService.cs index 1538b0cc..5de0e758 100644 --- a/BLAZAMSession/ApplicationUserStateService.cs +++ b/BLAZAMSession/ApplicationUserStateService.cs @@ -74,7 +74,8 @@ public ApplicationUserStateService(IHttpContextAccessor httpContextAccessor, IAp t = new Timer(Tick, UserStates, 60000, 60000); Task.Run(async () => { - Timeout = (await factory.CreateDbContextAsync()).AuthenticationSettings.FirstOrDefault()?.SessionTimeout; + using var context = await factory.CreateDbContextAsync(); + Timeout = context.AuthenticationSettings.FirstOrDefault()?.SessionTimeout; }); } diff --git a/BLAZAMUpdate/Services/AutoUpdateService.cs b/BLAZAMUpdate/Services/AutoUpdateService.cs index bb400521..c22096cc 100644 --- a/BLAZAMUpdate/Services/AutoUpdateService.cs +++ b/BLAZAMUpdate/Services/AutoUpdateService.cs @@ -43,6 +43,8 @@ public AutoUpdateService(IAppDatabaseFactory factory, UpdateService updateServic private void CleanDirectories(object? state) { + using var context = factory.CreateDbContext(); + var oldUpdateFiles = ApplicationUpdate.UpdateDownloadDirectory.Files; foreach (var file in oldUpdateFiles) { @@ -62,7 +64,7 @@ private void CleanDirectories(object? state) { Loggers.UpdateLogger.Warning("Attempting Update credentials to delete old update file: " + file); - var impersonation = factory.CreateDbContext().AppSettings.FirstOrDefault()?.CreateUpdateImpersonator(); + var impersonation = context.AppSettings.FirstOrDefault()?.CreateUpdateImpersonator(); if (impersonation != null && !impersonation.Run(() => { if (file.Writable) @@ -73,7 +75,7 @@ private void CleanDirectories(object? state) return false; })) { - impersonation = factory.CreateDbContext().ActiveDirectorySettings.FirstOrDefault()?.CreateDirectoryAdminImpersonator(); + impersonation = context.ActiveDirectorySettings.FirstOrDefault()?.CreateDirectoryAdminImpersonator(); if (impersonation != null && !impersonation.Run(() => { if (file.Writable) @@ -128,7 +130,7 @@ private void CleanDirectories(object? state) { Loggers.UpdateLogger.Warning("Attempting Update credentials to delete old staging files"); - var impersonation = factory.CreateDbContext().AppSettings.FirstOrDefault()?.CreateUpdateImpersonator(); + var impersonation = context.AppSettings.FirstOrDefault()?.CreateUpdateImpersonator(); if (impersonation != null && !impersonation.Run(() => { if (dir.Writable) @@ -141,7 +143,7 @@ private void CleanDirectories(object? state) return false; })) { - impersonation = factory.CreateDbContext().ActiveDirectorySettings.FirstOrDefault()?.CreateDirectoryAdminImpersonator(); + impersonation = context.ActiveDirectorySettings.FirstOrDefault()?.CreateDirectoryAdminImpersonator(); if (impersonation != null && !impersonation.Run(() => { if (dir.Writable) @@ -181,12 +183,13 @@ private void CleanDirectories(object? state) private async void CheckForUpdate(object? state) { + using var context = factory.CreateDbContext(); IJob updateCheckJob = new Job("Check for Update"); IJobStep checkForUpdateStep = new JobStep("Execute", async (step) => { try { - var appSettings = (await factory.CreateDbContextAsync()).AppSettings.FirstOrDefault(); + var appSettings = context.AppSettings.FirstOrDefault(); if (appSettings != null) { Loggers.UpdateLogger.Information("Checking for automatic update");