As I get it in RC2 there\'s a support for hosting applications within Windows Services. I tried to test it on a simple web api project (using .NET Framework 4.6.1).
If it is okay to run on full .NET Framework only, the previous answers are sufficient (Inheriting from ServiceBase
or using Microsoft's WebHostService
).
If, however, you have / want to run on .NET Core only (e.g. on windows nano server, single binary that works on linux as normal app and as service on windows, or need to run side-by-side with apps on an old .NET Framework version so you can't update the system framework), you'd have to make the appropriate windows API calls yourself via P/Invokes. Since i had to do that, i created a library that does exactly that.
There is a sample available that is also able to install itself when run with administrative privileges (e.g. dotnet MyService.dll --register-as-service
).