Net5 上的 ServiceProcessInstaller 在哪里? [英] Where is ServiceProcessInstaller on Net5?

查看:22
本文介绍了Net5 上的 ServiceProcessInstaller 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我使用了类 安装程序ServiceInstallerServiceProcessInstaller 使我的应用程序可自行安装.我只需要运行 InstallUtil.exe MyApp 即可将该应用程序安装为 Windows 服务.

In the past I used the classes Installer, ServiceInstaller and ServiceProcessInstaller to make my app self-installable. I just had to run InstallUtil.exe MyApp to install the app as a Windows Service.

但我在 DotNet5 上找不到这些类.

But I can't find those classes on DotNet5.

它们不会被移植?还有其他方法可以代替它们吗?任何人都可以向我指出一些有关如何实现这一目标的文档吗?

They won't be ported? There are any other approach to replace them? Can anyone point me to some documentation on how to achieve this goal?

以下是过去如何使用这些类的示例:

Here goes an example on how those classes were used in the past:

[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();
  }
}

推荐答案

在 .NET Core 之上创建 Windows Service 与基于 .NET Framework 创建 Windows Service 不同,因为 Windows Service 所需的所有基础架构,例如安装程序(不要与 MSI 安装程序混淆)在 .NET Core SDK 中默认不再可用.

Creating Windows Service on top of .NET Core is different from creating Windows Service based on .NET Framework, because all of the required infrastructure for Windows Service such as Installer (not to be confused with MSI installer) is no longer available by default in .NET Core SDK.

这是有充分理由的,因为默认情况下 .NET Core SDK 是跨平台的.因此,对于特定于操作系统/平台的支持,通常在 .NET Core SDK 之外以 nuget 包的形式提供.

This is for good reason, as by default .NET Core SDK is cross platform. Therefore for OS/platform specific, the supports are usually available outside the .NET Core SDK as nuget packages.

要在 .NET Core 中创建 Windows 服务,该服务必须在充当 Windows 服务的运行时主机中运行.为了支持这一点,您需要在代码库中添加 Microsoft.Extensions.Hosting.WindowsServices.此 nuget 将为您提供 Windows 服务的主机环境.

To create Windows Service in .NET Core, the service must run within a runtime host that act as Windows Service. To support this, you need to add Microsoft.Extensions.Hosting.WindowsServices in your code base. This nuget will provide you the host environment for Windows Service.

Windows 开发团队的官方博客中提供了详细步骤:https://devblogs.microsoft.com/ifdef-windows/creating-a-windows-service-with-c-net5/

The detail steps are available in this official blog from Windows dev team: https://devblogs.microsoft.com/ifdef-windows/creating-a-windows-service-with-c-net5/

注意:该博客适用于 .NET Core 3.1 和 .NET 5.0.

NOTE: that blog is applicable for both .NET Core 3.1 and .NET 5.0.

这篇关于Net5 上的 ServiceProcessInstaller 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆