I'm using .NET Core 2.2, and I would like to run a long time running background task (like the old Windows services). I would like to use Microsoft.Extensions.Hosting.BackgroundService (in Microsoft.Extensions.Hosting.Abstractions assembly).
My problem is that my service starts and stops immediately. I wrote this little sample to show what I've made:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MyHostedService
{
internal static class Program
{
private static void Main()
{
new HostBuilder()
.ConfigureServices((hostContext, services) => { services.AddHostedService<MyService>(); })
.Build()
.RunAsync();
}
}
public class MyService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
int count = 0;
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine($"Hit count: {++count}, IsCancellationRequested: {stoppingToken.IsCancellationRequested}");
await Task.Delay(TimeSpan.FromSeconds(1));
}
Console.WriteLine($"IsCancellationRequested: {stoppingToken.IsCancellationRequested}");
Console.WriteLine("The end.");
}
}
}
And here is the output:
> Hit count: 1, IsCancellationRequested: False
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: C:\Users\(personnal path hidden)\MyHostedService\MyHostedService\bin\Debug\netcoreapp2.2\
> IsCancellationRequested: True
> The end.
C:\Program Files\dotnet\dotnet.exe (process 31088) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
I didn't press Ctrl+C, nor escape key, I promise.
Do I miss something? Could you tell me what to change to ensure the service will run until I stop it by pressing Ctrl+C?
I expect to get a result that looks like:
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: C:\Users\(personnal path hidden)\MyHostedService\MyHostedService\bin\Debug\netcoreapp2.2\
> Hit count: 1, IsCancellationRequested: False
> Hit count: 2, IsCancellationRequested: False
> Hit count: 3, IsCancellationRequested: False
> Hit count: 4, IsCancellationRequested: False
> Hit count: 5, IsCancellationRequested: False
> Hit count: 6, IsCancellationRequested: False
> Hit count: 7, IsCancellationRequested: False
> Hit count: 8, IsCancellationRequested: False
> Hit count: 9, IsCancellationRequested: False
> Hit count: 10, IsCancellationRequested: False
> Hit count: 11, IsCancellationRequested: False
... ( now I press Ctrl+C ) ...
> IsCancellationRequested: True
> The end.
C:\Program Files\dotnet\dotnet.exe (process 31088) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .