32

Is there a way to develop a windows service (able to start on boot etc...) with .NET Core?

All tutorials and instructions I find utilize System.ServiceProcess.ServiceBase which can not be found and added for some reason in Visual Studio 2015?

I also try to avoid using 3rd party tools/libraries like SrvStart. Something like Topshelf would be acceptable but seems to be not available for .NET core.

And it would be great if the service could run under windows and linux.

Any ideas how I could achieve this?

monty
  • 7,888
  • 16
  • 63
  • 100
  • Thank you for the information. Though my goal is rather a background woker than a hosted web service. – monty Dec 13 '16 at 09:37
  • 2
    I've created a library that should simplify this process for you. You can find it here https://github.com/PeterKottas/DotNetCore.WindowsService. Hope you'll find it useful. – Peter Kottas Feb 21 '17 at 20:30
  • 1
    There are two different things you can use. https://github.com/dasMulli/dotnet-win32-service https://github.com/aspnet/Hosting/tree/dev/src/Microsoft.AspNetCore.Hosting.WindowsServices – TerribleDev Dec 09 '16 at 13:33

1 Answers1

23

I'll summarise some options:

  1. Move your code into a .NET Standard library, and host it in a .NET Framework app, so you can use ServiceBase. This will of course need the .NET Framework to be installed on the target machine
  2. Use NSSM (the Non-Sucking Service Manager) to manage a .NET Core console app (it has a public domain license)
  3. Use Windows API calls to hook into Windows service methods. This is the approach taken by DotNetCore.WindowsService and dotnet-win32-service (both are MIT licensed)
Cocowalla
  • 13,822
  • 6
  • 66
  • 112
  • 1
    I went with PeterKottas DotNetCore.WindowsService and was satisfied. – monty Nov 06 '18 at 06:45
  • @Cocowalla: Not only will that require the .NET Framwork to be installed, it will also require the .NET Framwork of a specific minimum version installed (since NetStandard doesn't work with .NET2 or .NET 4) ... – Stefan Steiger Apr 18 '19 at 10:03
  • @StefanSteiger I presume you are referring to my first option? Yes, it will require at least 4.6.2 if you want good compatibility. Is anyone still using .NET 2.0?! – Cocowalla Apr 18 '19 at 10:58
  • @Cocowalla: Yes, my company xD. – Stefan Steiger Apr 18 '19 at 11:31
  • since .Net Core 3: https://csharp.christiannagel.com/2019/10/15/windowsservice/ – monty Oct 15 '19 at 20:20