aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
553 stars 312 forks source link

IApplicationLifetime.StopApplication have no influence on IHostedService cancellation tokens #1513

Closed Eilon closed 5 years ago

Eilon commented 6 years ago

From @v-zubritsky on August 3, 2018 13:39

I've encountered an unexpected behavior with IHostedService implementation, cancellation tokens passed to it and IApplicationLifetime interface.

I have two hosted services, both of which perform some initialization on start, and if it succeeds, they do their work in background. But if initialization fails, I'd like to stop the application at all, as far as this background work is critical.

I inject IApplicationLifetime into services and call StopApplication on it in case of initialization failure. Application does stop, but cancellation tokens, passed to my IHostedServices and provided further for graceful shutdown handling, are not cancelled for some reason.

I have to link those tokens with IApplicationLifetime.ApplicationStopping to handle this issue, but this behavior doesn't seem too obvious.

public Task StartAsync(CancellationToken cancellationToken)
{
    var stoppingTokenSource = 
        CancellationTokenSource.CreateLinkedTokenSource(
            cancellationToken,       
            this.applicationLifetime.ApplicationStopping);
    ...
}

Repo to see this behavior reproduced is available by link.

Microsoft.AspNetCore.App 2.1.1 is used

dotnet --info outputs

.NET Core SDK (reflecting any global.json):
 Version:   2.1.302
 Commit:    9048955601

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.302\

Host (useful for support):
  Version: 2.1.2
  Commit:  811c3ce6c0

.NET Core SDKs installed:
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.302 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

Copied from original issue: aspnet/Home#3384

Drawaes commented 6 years ago

Correct me if I am wrong but the IHostedService is transient. So it won't exist anymore once you have completed the start. You need a Singleton that the IHostedService calls some method on if you want to persist anything passed the start call.

Tratcher commented 6 years ago

The WebHost holds onto the resolved instances and is responsible for stopping them.

aspnet-hello commented 5 years ago

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.