From 4b940d4bb6a209d0a5b33c157a2955b820a94cad Mon Sep 17 00:00:00 2001 From: Christian Siewert Date: Tue, 24 Oct 2023 17:39:32 +0200 Subject: [PATCH] Fix `DatabaseController`/`ModelSetupExecutor` by awaiting `Execute()` Not awaiting the `setup.Execute` call in `ModelSetupExecutor.Execute`. Otherwise the UOWs DbContext would get disposed before `setup.Execute` had finished and thus result in a DisposedObjectException. --- src/Moryx.Model/ModelSetupExecutor.cs | 4 ++-- .../Databases/Endpoint/DatabaseController.cs | 4 ++-- .../Databases/Controller/ModelSetupTests.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Moryx.Model/ModelSetupExecutor.cs b/src/Moryx.Model/ModelSetupExecutor.cs index 97598f22e..a0667529d 100644 --- a/src/Moryx.Model/ModelSetupExecutor.cs +++ b/src/Moryx.Model/ModelSetupExecutor.cs @@ -41,12 +41,12 @@ public ModelSetupExecutor(IDbContextManager dbContextManager) public IReadOnlyList GetAllSetups() => _setups; /// - public Task Execute(IDatabaseConfig config, IModelSetup setup, string setupData) + public async Task Execute(IDatabaseConfig config, IModelSetup setup, string setupData) { var unitOfWorkFactory = new UnitOfWorkFactory(_dbContextManager); using var uow = unitOfWorkFactory.Create(config); - return setup.Execute(uow, setupData); + await setup.Execute(uow, setupData); } } } \ No newline at end of file diff --git a/src/Moryx.Runtime.Endpoints/Databases/Endpoint/DatabaseController.cs b/src/Moryx.Runtime.Endpoints/Databases/Endpoint/DatabaseController.cs index 8de99bb23..6b6469fa2 100644 --- a/src/Moryx.Runtime.Endpoints/Databases/Endpoint/DatabaseController.cs +++ b/src/Moryx.Runtime.Endpoints/Databases/Endpoint/DatabaseController.cs @@ -227,7 +227,7 @@ public async Task> MigrateDatabaseModel(s [HttpPost("{targetModel}/setup")] [Authorize(Policy = RuntimePermissions.DatabaseCanSetup)] - public ActionResult ExecuteSetup(string targetModel, ExecuteSetupRequest request) + public async Task> ExecuteSetup(string targetModel, ExecuteSetupRequest request) { var contextType = _dbContextManager.Contexts.First(c => TargetModelName(c) == targetModel); var targetConfigurator = _dbContextManager.GetConfigurator(contextType); @@ -250,7 +250,7 @@ public ActionResult ExecuteSetup(string targetModel, Execute // ReSharper disable once SuspiciousTypeConversion.Global try { - setupExecutor.Execute(config, targetSetup, request.Setup.SetupData); + await setupExecutor.Execute(config, targetSetup, request.Setup.SetupData); return new InvocationResponse(); } catch (Exception ex) diff --git a/src/Tests/Moryx.Runtime.Endpoints.IntegrationTests/Databases/Controller/ModelSetupTests.cs b/src/Tests/Moryx.Runtime.Endpoints.IntegrationTests/Databases/Controller/ModelSetupTests.cs index bba7cde02..e8e1c5ce2 100644 --- a/src/Tests/Moryx.Runtime.Endpoints.IntegrationTests/Databases/Controller/ModelSetupTests.cs +++ b/src/Tests/Moryx.Runtime.Endpoints.IntegrationTests/Databases/Controller/ModelSetupTests.cs @@ -15,7 +15,7 @@ namespace Moryx.Runtime.Endpoints.IntegrationTests.Databases.Controller internal class ModelSetupTests { private IDbContextManager? _dbContextManager; - private DatabaseController? _databaseController; + private DatabaseController _databaseController; private readonly List _exceptions = new List(); [SetUp] @@ -34,7 +34,7 @@ public void Setup() } [Test] - public void ExecuteSetupDoesNotThrowDisposedObjectException() + public async Task ExecuteSetupDoesNotThrowDisposedObjectException() { // Add unobserved task exceptions to a list, to be checked later. TaskScheduler.UnobservedTaskException += (sender, e) => @@ -42,7 +42,7 @@ public void ExecuteSetupDoesNotThrowDisposedObjectException() _exceptions.Add(e.Exception); }; - var result = _databaseController?.ExecuteSetup("Moryx.TestTools.Test.Model.TestModelContext", new() + var result = await _databaseController!.ExecuteSetup("Moryx.TestTools.Test.Model.TestModelContext", new() { Config = new() {