Net5 上的 ServiceProcessInstaller 在哪里?

问题描述 投票:0回答:3

过去,我使用类 InstallerServiceInstallerServiceProcessInstaller 来使我的应用程序可自行安装。 我只需运行

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();
  }
}
c# windows windows-services .net-5
3个回答
1
投票

我们可以在 Assembly Core .System.ServiceProcess nuget 包中找到这些类/库,该包与 .Net 5、6 和最新版本兼容。在 Visual Studio 中将这个 Nuget 包安装到所需的项目上。


0
投票

在 .NET Core 之上创建 Windows 服务与基于 .NET Framework 创建 Windows 服务不同,因为默认情况下不再提供 Windows 服务所需的所有基础架构,例如安装程序(不要与 MSI 安装程序混淆) .NET Core 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。


-1
投票

我在从 NET Framework 4.8 迁移到 .NET6 的过程中也遇到了同样的问题。

找不到像 ServiceProcessInstaller/ServiceInstaller/ServiceAccount 这样的类。

经过进一步调查,我发现并添加了这个 NuGet: https://www.nuget.org/packages/Core.System.ServiceProcess 并且一切都按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.