我使用此示例创建了 ASP.NET Core Windows 服务应用程序: https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/host-and-deploy/windows-service/samples/2.x/ AspNetCore服务
现在,我想使用命令行安装我的服务,例如“MyService.exe -install”。我知道如果我使用“sc create”就会起作用,但我想使用我自己的应用程序来安装和配置我想要的服务。
我找到了一个使用 Toshelf 包的示例,但我正在使用 WebHostBuilder 并且尝试使用自定义服务配置。服务安装了,但是当我启动时它不起作用。
我正在使用 .NET Core 2.2。
我的配置:
HostFactory.Run(x =>
{
x.Service<CustomWebHostService>(sc =>
{
sc.ConstructUsing(() => new CustomWebHostService(host));
// the start and stop methods for the service
sc.WhenStarted(ServiceBase.Run);
sc.WhenStopped(s => s.Stop());
});
x.RunAsLocalSystem();
x.StartAutomatically();
x.SetServiceName("Teste Core");
x.SetDisplayName("Teste ASP.NET Core Service");
x.SetDescription("Teste ASP.NET Core as Windows Service.");
});
Run Code Online (Sandbox Code Playgroud)
我的完整源代码: https://github.com/rkiguti/dotnetcore.windows-service