diff --git a/src/Akka.Hosting/AkkaHostedService.cs b/src/Akka.Hosting/AkkaHostedService.cs index 39998333..13fce1f6 100644 --- a/src/Akka.Hosting/AkkaHostedService.cs +++ b/src/Akka.Hosting/AkkaHostedService.cs @@ -14,6 +14,7 @@ namespace Akka.Hosting internal sealed class AkkaHostedService : IHostedService { private ActorSystem _actorSystem; + private CoordinatedShutdown _coordinatedShutdown; // grab a reference to CoordinatedShutdown early private readonly IServiceProvider _serviceProvider; private readonly AkkaConfigurationBuilder _configurationBuilder; private readonly IHostApplicationLifetime _hostApplicationLifetime; @@ -33,6 +34,7 @@ public async Task StartAsync(CancellationToken cancellationToken) try { _actorSystem = _serviceProvider.GetRequiredService(); + _coordinatedShutdown = CoordinatedShutdown.Get(_actorSystem); await _configurationBuilder.StartAsync(_actorSystem); async Task TerminationHook() @@ -57,7 +59,7 @@ async Task TerminationHook() public async Task StopAsync(CancellationToken cancellationToken) { // run full CoordinatedShutdown on the Sys - await CoordinatedShutdown.Get(_actorSystem).Run(CoordinatedShutdown.ClrExitReason.Instance) + await _coordinatedShutdown.Run(CoordinatedShutdown.ClrExitReason.Instance) .ConfigureAwait(false); } }