diff --git a/src/Diagnostics/Properties/launchSettings.json b/src/Diagnostics/Properties/launchSettings.json index 9af281d..05c75f6 100644 --- a/src/Diagnostics/Properties/launchSettings.json +++ b/src/Diagnostics/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Diagnostics": { "commandName": "Project", - "commandLineArgs": "diagnosticjob --milliseconddelayperitem 10 --numberofitems 5000 --throwexceptioningetitemidasync" + "commandLineArgs": "diagnosticjob --milliseconddelayperitem 10 --numberofitems 5000" } } } \ No newline at end of file diff --git a/src/Runly/JobHost.cs b/src/Runly/JobHost.cs index 9a87a9c..50caa9f 100644 --- a/src/Runly/JobHost.cs +++ b/src/Runly/JobHost.cs @@ -133,11 +133,10 @@ public static Task RunJobAsync(this IHost host) /// The that represents the asynchronous operation. public static Task RunJobAsync(this IHost host, CancellationToken cancellationToken) { - using var scope = host.Services.CreateAsyncScope(); - + var scope = host.Services.CreateAsyncScope(); var action = scope.ServiceProvider.GetRequiredService(); - return action?.RunAsync(cancellationToken) ?? Task.CompletedTask; + return action?.RunAsync(cancellationToken).ContinueWith(async action => await scope.DisposeAsync()) ?? Task.CompletedTask; } } } diff --git a/test/Runly.Tests/Dependencies.cs b/test/Runly.Tests/Dependencies.cs index 3075af8..55d0a4a 100644 --- a/test/Runly.Tests/Dependencies.cs +++ b/test/Runly.Tests/Dependencies.cs @@ -1,8 +1,25 @@ -namespace Runly.Tests +using System; +using System.Threading.Tasks; + +namespace Runly.Tests { public interface IDep1 { } - public class Dep1 : IDep1 { } - public interface IDep2 { } + public class Dep1 : IDep1, IAsyncDisposable + { + public static bool IsDisposed { get; set; } + + public Dep1() + { + IsDisposed = false; + } + + public ValueTask DisposeAsync() + { + IsDisposed = true; + return ValueTask.CompletedTask; + } + } + public interface IDep2 { } public class Dep2 : IDep2 { } public class Dep3 { } public class Dep4 { } diff --git a/test/Runly.Tests/Scenarios/Running/Running_a_job.cs b/test/Runly.Tests/Scenarios/Running/Running_a_job.cs index a90251e..126d210 100644 --- a/test/Runly.Tests/Scenarios/Running/Running_a_job.cs +++ b/test/Runly.Tests/Scenarios/Running/Running_a_job.cs @@ -46,7 +46,15 @@ public async Task should_run_a_job_with_scoped_dependency_in_constructor() }) .Build(); - await action.RunJobAsync(); + Dep1.IsDisposed.Should().BeFalse(); + + var run = action.RunJobAsync(); + + Dep1.IsDisposed.Should().BeFalse(); + + await run; + + Dep1.IsDisposed.Should().BeTrue(); } } }