【发布时间】:2020-07-17 22:35:26
【问题描述】:
我使用此示例创建了一个 ASP.NET Core Windows 服务应用程序: https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/host-and-deploy/windows-service/samples/2.x/AspNetCoreService
现在,我想使用命令行安装我的服务,例如“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.");
});
我的完整源代码: https://github.com/rkiguti/dotnetcore.windows-service
【问题讨论】:
-
知道如何在 .NET Core 3.1 上实现同样的效果吗?
标签: asp.net-core windows-services topshelf