« Service et csharp » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 51 : Ligne 51 :
# Right-click on {{boxx|MyService}} project → Properties
# Right-click on {{boxx|MyService}} project → Properties
# Application tab → Startup object = {{boxx|MyService.Program}}
# Application tab → Startup object = {{boxx|MyService.Program}}
= [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#install-the-service Install the service] =
# Open {{boxx|Developer Command Prompt for VS}} with administrative credentials
<kode lang='msdos'>
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug
installutil MyService.exe
</kode>

Version du 11 février 2022 à 22:49

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

  1. Open Developer Command Prompt for VS with administrative credentials
Fichier:Msdos.svg
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug

installutil MyService.exe