6

So i have an windows service application and want to start porting it to .NET Core. The application is long running with use of sockets,self hosted mvc, timers etc. I am planning to convert the application to .NET core in order to be able to run it on Windows/Linux etc. The question is what is correct way to implement the service code ? For example i will need to have a web host that serves MVC/API plus the actual service implementation that handles sockets,events etc. I found that currently the way to go is to inherit the service code class from BackgroundService https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.backgroundservice?view=aspnetcore-3.0 So the question is whether BackgroundService is what i should be looking at or is there another more appropriate way?

Thanks.

NullReference
  • 862
  • 1
  • 11
  • 27
  • A very generall question, but hard to make it concreate. Also I havnt seen any answers to this anywhere. – Prophet Lamb Sep 25 '19 at 09:11
  • A later question has [an answer](https://stackoverflow.com/a/70601727/1178314) covering the case of hosting Asp.Net Core inside a Windows Service. – Frédéric Mar 15 '23 at 17:03

2 Answers2

1

Another option is to use Hangfire (https://www.hangfire.io/)

This works well for background/scheduled processing and works across multiple VMs etc.

Is it not possible to create windows services in .Net core (or that your question)

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • A [NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Hosting.WindowsServices/3.0.0) made by Microsoft for creating Windows services was released two days before your answer, and was released in beta since six months. So, this was already possible, but fresh. See current documentation [here](https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service). – Frédéric Mar 31 '23 at 23:00
0

See Microsoft documentation, yes, using a BackgroundService worker is one way to go for the general case of creating some random windows service with .Net Core.

For your case, which asks for a service which is a Web server too, additional steps are required, as answered on a later question.

To use a worker, a good starting point is the "Worker Service" project template.

Instead of deriving from BackgroundService, you may directly implement the IHostedService interface. That allows you to implements a StartAsync and a StopAsync, instead of having to loop/yield the thread inside a single ExecuteAsync method, if that is more convenient for your use case.

The project needs a reference to the NuGet package Microsoft.Extensions.Hosting.WindowsServices for being executed as a Windows Service.

Then in your Program.cs, add a call to .UseWindowsService(). Your Program.cs code should look like this :

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddHostedService<YourWorker>();
    })
    .UseWindowsService()
    .Build();

await host.RunAsync();
Frédéric
  • 9,364
  • 3
  • 62
  • 112