Skip to content

Commit

Permalink
Fix DatabaseController/ModelSetupExecutor by awaiting Execute()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seveneleven committed Oct 25, 2023
1 parent 37d0fac commit 4b940d4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Moryx.Model/ModelSetupExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public ModelSetupExecutor(IDbContextManager dbContextManager)
public IReadOnlyList<IModelSetup> GetAllSetups() => _setups;

/// <inheritdoc />
public Task Execute(IDatabaseConfig config, IModelSetup setup, string setupData)
public async Task Execute(IDatabaseConfig config, IModelSetup setup, string setupData)
{
var unitOfWorkFactory = new UnitOfWorkFactory<TContext>(_dbContextManager);
using var uow = unitOfWorkFactory.Create(config);

return setup.Execute(uow, setupData);
await setup.Execute(uow, setupData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public async Task<ActionResult<DatabaseMigrationSummary>> MigrateDatabaseModel(s

[HttpPost("{targetModel}/setup")]
[Authorize(Policy = RuntimePermissions.DatabaseCanSetup)]
public ActionResult<InvocationResponse> ExecuteSetup(string targetModel, ExecuteSetupRequest request)
public async Task<ActionResult<InvocationResponse>> ExecuteSetup(string targetModel, ExecuteSetupRequest request)
{
var contextType = _dbContextManager.Contexts.First(c => TargetModelName(c) == targetModel);
var targetConfigurator = _dbContextManager.GetConfigurator(contextType);
Expand All @@ -250,7 +250,7 @@ public ActionResult<InvocationResponse> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception> _exceptions = new List<Exception>();

[SetUp]
Expand All @@ -34,15 +34,15 @@ 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) =>
{
_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()
{
Expand Down

0 comments on commit 4b940d4

Please sign in to comment.