0

Currently I am using an ASP.NET Core Web API as a Windows service like this:

var builder = WebApplication.CreateBuilder();
builder.Host.UseWindowsService();
var app = builder.Build();
app.MapGet("/hello", () => "Hello World");

With older .NET Framework Windows services, I have the possibility to overwrite the OnPause and OnContinue functionality.

Now I need something similar for my API Windows service.

I need to be able to pause and continue the service and execute logic within these events.

I could not find anything regarding these requirements.

Does anyone have an idea if that is possible and how to realize that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Both OnPause and OnContinue are based on ServiceBase Class, which does not apply .NET Core.

When using the .NET Core program as a Windows service, you need to install the Microsoft.Extensions.Hosting.WindowsServices Nuget package to use the BackgroundService, which inherits from IHostedService. This does not include OnPause, OnContinue, etc., it only has ExecuteAsync, StartAsync and StopAsync.

Here are a few documents that can help you better understand the issues related to .NET Core as a Windows Service:

Difference between ExecuteAsync and StartAsync methods in BackgroundService .net core.

Background tasks with hosted services in ASP.NET Core.

Chen
  • 4,499
  • 1
  • 2
  • 9