Gug*_*der 6 c# windows windows-services .net-5
过去,我使用Installer、ServiceInstaller和ServiceProcessInstaller类来使我的应用程序可自行安装。我只需运行即可InstallUtil.exe MyApp将应用程序安装为 Windows 服务。
但我在 DotNet5 上找不到这些类。
他们不会被移植吗?还有其他方法可以替代它们吗?谁能向我指出一些有关如何实现这一目标的文档?
下面是一个关于过去如何使用这些类的示例:
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
private string serviceName = "MyApp";
public MyServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
processInstaller.Username = null;
processInstaller.Password = null;
serviceInstaller.ServiceName = serviceName;
serviceInstaller.DisplayName = serviceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
this.Committed += new InstallEventHandler(MyServiceInstaller_Committed);
}
void MyServiceInstaller_Committed(object sender, InstallEventArgs e)
{
var controller = new ServiceController(serviceName);
controller.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
在 .NET Core 之上创建 Windows 服务与基于 .NET Framework 创建 Windows 服务不同,因为默认情况下,Windows 服务所需的所有基础结构(例如安装程序(不要与 MSI 安装程序混淆))不再可用。 NET 核心 SDK。
这是有充分理由的,因为默认情况下 .NET Core SDK 是跨平台的。因此,对于特定于操作系统/平台的支持,通常可以在 .NET Core SDK 之外以 nuget 包的形式提供。
要在 .NET Core 中创建 Windows 服务,该服务必须在充当 Windows 服务的运行时主机中运行。为了支持这一点,您需要添加Microsoft.Extensions.Hosting.WindowsServices到您的代码库中。此 nuget 将为您提供 Windows 服务的主机环境。
详细步骤可参见 Windows 开发团队的官方博客: https://devblogs.microsoft.com/ifdef-windows/creating-a-windows-service-with-c-net5/
注意:该博客适用于 .NET Core 3.1 和 .NET 5.0。
| 归档时间: |
|
| 查看次数: |
4058 次 |
| 最近记录: |