.NET

Understanding Modern .NET Worker Services

Windows Services used to mean installer classes and InstallUtil.exe. What .NET 8 Worker Services replace, piece by piece, for VB.NET folks.

In the VB.NET 3.0 days, creating a Windows Service took a lot of ceremony. You started from the Visual Studio Service template, wrote a service installer class by hand, kept your settings as XML in App.config, and deployed with InstallUtil.exe, a temperamental command-line tool that demanded specific permissions. Even something as basic as a FileSystemWatcher usually ended up living inside a Windows Forms application.

In .NET 8 most of that is gone. There’s native Windows Service support with no installer classes, the XML configuration has given way to JSON in appsettings.json, and dependency injection is built in (if you’ve used Spring in Java it will look familiar), which makes a service easier to structure, maintain and test. What I like about it is that I get to spend my time on the code that solves the problem and not on the infrastructure around it.

This guide goes through it piece by piece for VB.NET folks.

What’s different from the old days

The old way (VB.NET 3.0):

  • Windows Forms Applications
  • Windows Services created through the Visual Studio Service template
  • App.config for settings
  • InstallUtil.exe for installation
  • Manual service installer classes
  • System.IO.FileSystemWatcher in a form

The new way (.NET 8):

  • No more InstallUtil.exe
  • No more service installer classes
  • appsettings.json in place of App.config
  • Built-in dependency injection (like Spring if you used Java)
  • Native Windows Service support

The new structure

Program.cs (think of this as your old Main() method)

// Old VB.NET way:
// Public Sub Main()
//     ServiceBase.Run(New MyService())
// End Sub

// New .NET way:
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();

Worker.cs (this is like your old Service1.vb)

// Old VB.NET way:
// Public Class Service1
//     Inherits ServiceBase
//     Private WithEvents FileWatcher As FileSystemWatcher

// New .NET way:
public class Worker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // Your service code here
    }
}

Configuration (replacing App.config)

// Old VB.NET way:
// 
//     
// 

// New .NET way (appsettings.json):
{
  "WatchSettings": {
    "FolderPath": "C:\\WatchFolder",
    "FileFilter": "*.txt"
  }
}

Installation

The old way (VB.NET 3.0):

' Required installer classes
Public Class ProjectInstaller
    Inherits System.Configuration.Install.Installer
End Class

' Installation command
installutil.exe MyService.exe

The new way (.NET 8):

# One command does it all
sc.exe create "TSVAutomation" binpath= "C:\Path\To\TSVAutomation.exe" start= auto

File watching

The old way (VB.NET 3.0):

Private WithEvents FileWatcher As New FileSystemWatcher
Private Sub SetupWatcher()
    FileWatcher.Path = "C:\WatchFolder"
    FileWatcher.Filter = "*.txt"
    FileWatcher.EnableRaisingEvents = True
End Sub

Private Sub FileWatcher_Created(sender As Object, e As FileSystemEventArgs)
    ' Handle new file
End Sub

The new way (.NET 8):

private FileSystemWatcher? _watcher;
public override Task StartAsync(CancellationToken cancellationToken)
{
    _watcher = new FileSystemWatcher
    {
        Path = _watchPath,
        Filter = _fileFilter,
        EnableRaisingEvents = true
    };
    _watcher.Created += OnFileCreated;
    return base.StartAsync(cancellationToken);
}

The differences to remember

There are no more project installers. The old way required special installer classes, and now you just use sc.exe commands. Configuration went from the XML-based App.config to the JSON-based appsettings.json. The service itself went from heavy ServiceBase inheritance to the lightweight BackgroundService class. And deployment went from InstallUtil.exe plus manual setup to dotnet publish plus sc.exe.

Quick start steps

  1. Create the project:
Old: File -> New -> Windows Service
New: Create Worker Service template
  1. Configure settings:
Old: Edit App.config
New: Edit appsettings.json
  1. Build:
Old: Build Solution
New: dotnet publish --configuration Release
  1. Install:
Old: installutil.exe MyService.exe
New: sc.exe create "ServiceName" binpath= "path\to\exe"

Common VB.NET to modern .NET translations

Event handlers:

' Old VB.NET
Private Sub FileWatcher_Created(sender As Object, e As FileSystemEventArgs)
// New .NET
private void OnFileCreated(object sender, FileSystemEventArgs e)

Reading configuration:

' Old VB.NET
Dim watchPath As String = ConfigurationManager.AppSettings("WatchFolder")
// New .NET
string watchPath = configuration.GetValue("WatchSettings:FolderPath")

The fundamental concepts haven’t changed. You’re still creating a Windows Service that watches for files, and what’s different is the setup, the configuration, and the deployment.


Have a .NET modernization project?

Free 30-minute call. No pitch, we just look at your migration together.

Get in touch