Where is ServiceProcessInstaller on Net5?

Viewed 1547

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.

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

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.

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.

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.

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/

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

Related