带有 .NET Core 的 Windows 服务 [英] Windows Service with .NET Core
问题描述
有没有办法使用 .NET Core 开发 Windows 服务(能够在启动时启动等...)?
Is there a way to develop a windows service (able to start on boot etc...) with .NET Core?
我发现的所有教程和说明都使用了 System.ServiceProcess.ServiceBase,但由于某些原因无法在 Visual Studio 2015 中找到和添加?
All tutorials and instructions I find utilize System.ServiceProcess.ServiceBase which can not be found and added for some reason in Visual Studio 2015?
我也尽量避免使用像 SrvStart 这样的 3rd 方工具/库.Topshelf 之类的东西是可以接受的,但似乎不适用于 .NET 核心.
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.
如果该服务可以在 windows 和 linux 下运行,那就太好了.
And it would be great if the service could run under windows and linux.
有什么想法可以实现吗?
Any ideas how I could achieve this?
推荐答案
我将总结一些选项:
- 将您的代码移动到 .NET Standard 库中,并将其托管在 .NET Framework 应用中,以便您可以使用
ServiceBase
.这当然需要在目标机器上安装 .NET Framework - 使用 NSSM(Non-Sucking Service Manager)来管理 .NET Core 控制台应用程序(它有一个公共域许可证)
- 使用 Windows API 调用挂钩到 Windows 服务方法.这是 DotNetCore.WindowsService 和 dotnet-win32-service(均获得 MIT 许可)
- 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 - Use NSSM (the Non-Sucking Service Manager) to manage a .NET Core console app (it has a public domain license)
- 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)
这篇关于带有 .NET Core 的 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!