.NET
Windows Service
Installation
Auto-start
Software Development

How to make a .NET Windows Service start right after the installation?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Making a Windows Service start after installation involves two separate concerns: how the service is registered with Windows, and what the installer does when installation finishes. Setting the startup type to automatic is not enough by itself, because automatic startup only guarantees behavior after the next boot unless you also start the service immediately.

Register the Service Correctly

In modern .NET projects, a Windows Service is usually a Worker Service hosted with UseWindowsService(). That gives the process the right lifetime behavior when the Service Control Manager starts it.

csharp
1using Microsoft.Extensions.DependencyInjection;
2using Microsoft.Extensions.Hosting;
3
4Host.CreateDefaultBuilder(args)
5    .UseWindowsService()
6    .ConfigureServices(services =>
7    {
8        services.AddHostedService<Worker>();
9    })
10    .Build()
11    .Run();

The worker itself should return quickly from startup and move long-running work into the background loop. If startup blocks for too long, Windows can mark the service as failed.

csharp
1using Microsoft.Extensions.Hosting;
2using Microsoft.Extensions.Logging;
3
4public sealed class Worker : BackgroundService
5{
6    private readonly ILogger<Worker> _logger;
7
8    public Worker(ILogger<Worker> logger) => _logger = logger;
9
10    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
11    {
12        while (!stoppingToken.IsCancellationRequested)
13        {
14            _logger.LogInformation("Service heartbeat at {Time}", DateTimeOffset.Now);
15            await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
16        }
17    }
18}

That code makes the program service-friendly, but it still does not answer the installer question. The installer must register the service and optionally start it.

Start It During Installation

If you install with sc.exe, PowerShell, WiX, or another setup tool, the key idea is the same:

  1. Register the service.
  2. Set its startup mode.
  3. Start it once installation succeeds.

For a simple manual installation, this pattern works:

powershell
sc.exe create DemoWorker binPath= "C:\Services\DemoWorker\DemoWorker.exe" start= auto
sc.exe description DemoWorker "Background processing service"
sc.exe start DemoWorker

The first command creates the service entry, start= auto tells Windows to start it automatically on future boots, and the last command starts it immediately without waiting for a reboot.

If you are using WiX, the same concept is usually expressed with ServiceInstall plus ServiceControl. ServiceInstall registers the service, while ServiceControl can tell MSI to start it after the files are copied.

xml
1<Component Id="ServiceComponent" Guid="*">
2  <File Id="ServiceExe" Source="DemoWorker.exe" KeyPath="yes" />
3  <ServiceInstall
4      Id="InstallService"
5      Name="DemoWorker"
6      DisplayName="Demo Worker"
7      Start="auto"
8      Type="ownProcess"
9      ErrorControl="normal" />
10  <ServiceControl
11      Id="StartService"
12      Name="DemoWorker"
13      Start="install"
14      Stop="both"
15      Remove="uninstall"
16      Wait="yes" />
17</Component>

That is the usual answer when someone says, "make the service start right after installation." The service itself cannot reliably decide that. The installer must request it.

Verify the Startup Behavior

After installation, verify both the current state and the configured startup mode. Those are different properties.

powershell
Get-Service -Name DemoWorker | Select-Object Name, Status, StartType

A healthy result should show Status as Running and StartType as Automatic. If the service is installed as automatic but still shows Stopped, the installer probably never issued the start command, or the service crashed during startup.

You can also start it from managed code in an admin tool or custom action, although external installer support is usually cleaner:

csharp
1using System.ServiceProcess;
2
3using var controller = new ServiceController("DemoWorker");
4if (controller.Status == ServiceControllerStatus.Stopped)
5{
6    controller.Start();
7    controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
8}

Common Pitfalls

The most common mistake is assuming Automatic means "start immediately after install." It actually means "start automatically when Windows boots." If you need instant startup, call StartService, sc.exe start, or the equivalent installer action.

Another common issue is heavy work inside startup. A Windows Service should finish startup quickly and move real processing into background execution. Slow database migrations, network warmup, or blocking loops can cause timeout errors.

Permissions also matter. Installing and starting services requires administrative privileges. A setup package that runs without elevation may copy files successfully but fail when creating or starting the service.

Finally, confirm paths and dependencies. If the executable path is wrong, required configuration files are missing, or a dependent service is not running, the install may appear successful while the service never transitions to Running.

Summary

  • A Windows Service must be both registered and explicitly started if you want it running immediately after installation.
  • 'UseWindowsService() makes a .NET worker behave correctly under the Service Control Manager.'
  • 'start= auto controls future boot behavior, not immediate startup by itself.'
  • Installer tooling such as sc.exe, PowerShell, or WiX should include a post-install start step.
  • Always verify both Status and StartType after installation.

Course illustration
Course illustration

All Rights Reserved.