Your first Windows Service

1. Create a New Windows Service Project

  1. Open Visual Studio and create a new project
  2. Template selection: Visual C# → Windows → Windows Service
  3. Set the project name and save path, then click OK

Note: Newer Visual Studio versions no longer include this built‑in template. It is only supported for .NET Framework; for .NET Core/.NET 5+, the Worker Service template is recommended.

2. Project Structure Overview

Core auto‑generated files:

  1. Program.cs: Application entry point
  2. Service1.cs: Main service logic class
1. Program.cs Entry Code
static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new Service1()
        };
        ServiceBase.Run(ServicesToRun);
    }
}Code language: JavaScript (javascript)

Purpose: Starts the service host and loads one or more service instances.

2. Service1.cs Main Service Class
public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    // Executed when the service starts
    protected override void OnStart(string[] args)
    {

    }

    // Executed when the service stops
    protected override void OnStop()
    {

    }
}
  • OnStart: Triggered on service startup, place your business logic here
  • OnStop: Triggered on service shutdown, used for resource cleanup

3. Add Installer

This step is mandatory! Without it you cannot register as a system service.

Operation Steps
  1. On the Service1 designer view, Right‑click → Add Installer
  2. Visual Studio auto‑generates ProjectInstaller.cs with two core components:
    • serviceInstaller1
    • serviceProcessInstaller1

Component Property Configuration

  1. serviceInstaller1 (Service Information Configuration)
    • ServiceName: Internal service identifier (used in code)
    • DisplayName: Name shown in service manager
    • Description: Service description
    • StartType: Startup type Manual / Automatic
  2. serviceProcessInstaller1 (Run‑as Account Configuration)
    • Account: Execution identity
    • LocalSystem: Local system account (highest privileges, recommended for testing)
    • User: Specified Windows account (default, requires username and password)

4. Implement Test Business Logic

Your business code belongs inside Service1. Place your custom logic here. Since Windows services are hard to debug, we will test by writing log output as a temporary workaround. Installation and uninstallation are required.

Modify Service1.cs

using System.IO;

protected override void OnStart(string[] args)
{
    // Write log on startup
    File.AppendAllText("log.txt", "Startup Time:" + DateTime.Now + Environment.NewLine);
}

protected override void OnStop()
{
    // Write log on shutdown
    File.AppendAllText("log.txt", "Stop Time:" + DateTime.Now + Environment.NewLine);
}Code language: JavaScript (javascript)

5. Compile, Install and Start the Service

  1. Build the project and get WindowsServiceDemo.exe
  2. Register service using InstallUtil.exe (Administrator CMD)
# Install service
InstallUtil.exe WindowsServiceDemo.exe

# Uninstall service
InstallUtil.exe /u WindowsServiceDemo.exeCode language: PHP (php)
  1. Open service manager services.msc, locate your service, start / stop it and check log output.

Services cannot run like regular applications. You must use dedicated tools for installation.

Important Notes

  1. No direct F5 debugging: Windows services cannot be debugged directly. Recommended approach: implement dual‑mode logic; run as console app from command‑line, run as service after installation.
  2. File path issues: Relative paths do not work reliably; prefer absolute paths.
  3. Permission issues: The running account determines access to disks, networks and shared folders.
  4. Startup timeout: Long‑running blocking code inside OnStart will trigger system startup failure. Offload heavy tasks onto new threads.

Your first Windows Service

Leave a Reply

Your email address will not be published. Required fields are marked *