Service et csharp

De Banane Atomic
Aller à la navigationAller à la recherche

Links

Create the project

  1. File → New → Project
  2. Windows Service (.NET Framework)
  3. Rename Service1.cs to MyService.cs
  4. Double-click on MyService.cs to open the designer → Properties → ServiceName = MyService

Code

MyService.cs
public partial class MyService : ServiceBase
{
    public MyService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    { }

    protected override void OnStop()
    { }
}
Program.cs
static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
        new MyService()
    };
    ServiceBase.Run(ServicesToRun);
}

Installer

  1. Right-click on MyService.cs → View Designer
  2. Right-click on the background → Add Installer
  3. A new class ProjectInstaller has been added
  4. Right-click on ProjectInstaller.cs → View Designer
  5. Description = This service manages the ...
  6. DisplayName = My service
  7. StartType = Automatic
  8. Select serviceProcessInstaller1 → Properties → Account = LocalSystem

Build the service

  1. Right-click on MyService project → Properties
  2. Application tab → Startup object = MyService.Program

Install the service

InstallUtil

Ps.svg
# first deploy your service (MyService.exe) to the folder "C:\Program Files\MyService"
# install MyService
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe "C:\Program Files\MyService\MyService.exe"

# uninstall MyService
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u "C:\Program Files\MyService\MyService.exe"
MyServiceInstaller.cs
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
    public MyServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        var assemblyDescription = Assembly.GetExecutingAssembly()
            .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)
            .OfType<AssemblyDescriptionAttribute>().FirstOrDefault();

        processInstaller.Account = ServiceAccount.LocalSystem;

        serviceInstaller.ServiceName = typeof(MyService).Name;
        serviceInstaller.DisplayName = string.Concat(typeof(MyService).Name.Select(x => Char.IsUpper(x) ? $" {x}" : $"{x}")).TrimStart(' ');
        serviceInstaller.Description = assemblyDescription?.Description;
        serviceInstaller.StartType = ServiceStartMode.Manual;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);
    }
}
AssemblyInfo.cs
[assembly: AssemblyDescription("My Assembly description.")]

PowerShell

Open a powershell prompt with administrative credentials.

Ps.svg
cd $home\source\repos\MyService\MyService\bin\Debug

New-Service -Name "MyService" -DisplayName "My service" -Description "This service runs ..." -StartupType "Manual" -BinaryPathName MyService.exe

# uninstall, PowerShell 6 or later is required
Remove-Service -Name "MyService"

# after the executable for a service is deleted, the service might still be present in the registry
# remove the entry for the service from the registry
sc.exe delete "MyService"

Developer Command Prompt for VS

Open Developer Command Prompt for VS with administrative credentials.

Dos.svg
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug

installutil MyService.exe
REM %windir%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe

REM uninstall the service
installutil.exe /u MyService.exe

Create an installer package

  1. Install the Wix Toolset Visual Studio Extension