.net core开发Windows服务
查看asp.net core的Windows宿主的源代码Microsoft.AspNetCore.Hosting.WindowsServices,发现最终给宿主是继承自System.ServiceProcess.ServiceBase,同时实现了OnStart以及OnStop等一些列方法.我们知道dotnet framework 中的service基类也是System.ServiceProcess.ServiceBase。
我们新建一个dotnet console程序,添加一个windows服务,取名Stock
默认代码如下:
默认是会报dll缺失的错误,我们添加nuget引用System.ServiceProcess.ServiceController。
修改main方法,运行Stock即可
剩下的就是通过dotnet core命令安装部署了。