Net5 上的 ServiceProcessInstaller 在哪里?

Gug*_*der 6 c# windows windows-services .net-5

过去,我使用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();
  }
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*ono 0

在 .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。

  • 是的,你说得对。我明白你所说的,并且我做到了。但我无法完成的是将我的组件变成可自安装的,就像我过去所做的那样。这一切都是因为我无法找到替换安装程序类 Installer、ServiceInstaller 和 ServiceProcessInstaller 的方法。在 dotnet 框架时代,安装服务非常容易。也许我们现在必须手动完成这一切,就像乱搞 sc.exe,但我仍然希望找到一个自动化的解决方案。 (3认同)