From 8fc7bfcef7b1d3d3b932621c480a1e950605e409 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Sun, 13 Oct 2024 19:18:18 -0500 Subject: [PATCH] Make sure job DI scope is disposed --- src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs | 2 +- src/Foundatio.Extensions.Hosting/Startup/StartupExtensions.cs | 2 +- src/Foundatio/Jobs/JobRunner.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs index 29b32932..471c5691 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs @@ -143,7 +143,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default) try { using var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job " + Options.Name, ActivityKind.Server); - var scope = _serviceProvider.CreateScope(); + await using var scope = _serviceProvider.CreateAsyncScope(); var job = Options.JobFactory(scope.ServiceProvider); var result = await job.TryRunAsync(cancellationToken).AnyContext(); diff --git a/src/Foundatio.Extensions.Hosting/Startup/StartupExtensions.cs b/src/Foundatio.Extensions.Hosting/Startup/StartupExtensions.cs index fcc71fc3..ff171865 100644 --- a/src/Foundatio.Extensions.Hosting/Startup/StartupExtensions.cs +++ b/src/Foundatio.Extensions.Hosting/Startup/StartupExtensions.cs @@ -26,7 +26,7 @@ public static partial class StartupExtensions { public static async Task RunStartupActionsAsync(this IServiceProvider serviceProvider, CancellationToken shutdownToken = default) { - using var startupActionsScope = serviceProvider.CreateScope(); + await using var startupActionsScope = serviceProvider.CreateAsyncScope(); var sw = Stopwatch.StartNew(); var logger = startupActionsScope.ServiceProvider.GetService()?.CreateLogger("StartupActions") ?? NullLogger.Instance; var startupActions = startupActionsScope.ServiceProvider.GetServices().ToArray(); diff --git a/src/Foundatio/Jobs/JobRunner.cs b/src/Foundatio/Jobs/JobRunner.cs index 17ba14c7..574ee786 100644 --- a/src/Foundatio/Jobs/JobRunner.cs +++ b/src/Foundatio/Jobs/JobRunner.cs @@ -181,7 +181,7 @@ public async Task RunAsync(CancellationToken cancellationToken = default) { try { - var scope = _serviceProvider.CreateScope(); + await using var scope = _serviceProvider.CreateAsyncScope(); var jobInstance = _options.JobFactory(scope.ServiceProvider); await jobInstance.RunContinuousAsync(_options.Interval, _options.IterationLimit, cancellationToken).AnyContext();