Hosting ASP.NET Core as Windows service

前端 未结 7 1084
孤街浪徒
孤街浪徒 2020-12-04 08:43

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).

7条回答
  •  温柔的废话
    2020-12-04 09:17

    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).

提交回复
热议问题