TechBuzzPro

How to Create a Windows Service in .NET Framework

· 6 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To create a Windows Service in .NET Framework, you build a class that inherits from ServiceBase, override OnStart and OnStop, add an installer, then use InstallUtil.exe or sc.exe to register and start it. The service runs under a Windows account and starts automatically with the OS, with no user logged in. This guide walks through the full cycle in Visual Studio using C# and the .NET Framework (4.x), including common pitfalls and how to debug a service that refuses to start.

What You'll Need

  • Visual Studio (any edition that supports .NET Framework development; Community is fine).
  • .NET Framework 4.x targeting pack installed (usually included with Visual Studio).
  • Administrator rights on the machine where you will install and run the service.
  • A basic understanding of C# and the Windows Services snap-in (services.msc).

Step-by-Step

  1. Create the project. In Visual Studio, choose File > New > Project, select Windows Service (.NET Framework) under the C# Windows Desktop templates, name it (for example MyWorkerService), and click OK. If the template is missing, create a Console App (.NET Framework) and add a reference to System.ServiceProcess; then add a class that inherits ServiceBase.
  2. Open the service class. The template creates Service1.cs. Right-click it, choose View Code (or press F7), and rename the class and file to something meaningful, such as WorkerService. Update the InitializeComponent call and the class name consistently.
  3. Set the service name. In the designer (double-click WorkerService.cs), set the ServiceName property in the Properties window to a unique name, for example MyWorkerService. This is the name shown in services.msc and used by sc.exe; it must match the installer later.
  4. Override OnStart and OnStop. In code, override OnStart(string[] args) and OnStop(). Put long-running work on a background thread or timer, not directly in OnStart, because the Service Control Manager expects OnStart to return quickly (within about 30 seconds by default). A simple pattern is to start a System.Threading.Timer or a Task in OnStart and signal cancellation in OnStop.
  5. Add an installer. In the designer, right-click the design surface and choose Add Installer. This creates ProjectInstaller.cs with two components: serviceProcessInstaller1 and serviceInstaller1. Set serviceProcessInstaller1.Account to the account the service should run under (for example LocalSystem, NetworkService, or User with credentials). Set serviceInstaller1.ServiceName to the same name as the service class, and set StartType to Automatic or Manual.
  6. Build the project. Use Build > Build Solution. The output is an executable (for example MyWorkerService.exe) in bin\Debug or bin\Release. You will install this EXE, not the DLL.
  7. Install the service. Open a Command Prompt as Administrator, navigate to the folder containing InstallUtil.exe (typically C:\Windows\Microsoft.NET\Framework\v4.0.30319), and run:
    InstallUtil.exe "C:\path\to\MyWorkerService.exe"
    Alternatively, use sc.exe create MyWorkerService binPath= "C:\path\to\MyWorkerService.exe" start= auto (note the space after each equals sign).
  8. Start the service. Open services.msc, find your service by name, right-click it, and choose Start. Or from an elevated prompt run sc.exe start MyWorkerService or net start MyWorkerService.
  9. Verify and debug. Check the service status in services.msc and review the Windows Event Viewer under Windows Logs > Application for errors from your service. To debug, attach Visual Studio to the running service process (Debug > Attach to Process) or add temporary file logging in OnStart.
  10. Uninstall when needed. Stop the service first, then run InstallUtil.exe /u "C:\path\to\MyWorkerService.exe" or sc.exe delete MyWorkerService. Remove the EXE and any data files you created.

Common Mistakes

  • Doing heavy work in OnStart. The Service Control Manager times out if OnStart blocks; start a timer or task and return.
  • Mismatched service names. The ServiceName in the designer, the installer, and sc.exe commands must match exactly, including case.
  • Installing the DLL instead of the EXE. InstallUtil needs the built executable.
  • Running without administrator rights. Installation and service control require elevation.
  • Forgetting to set the account. The default may be LocalSystem; if your service needs network or user resources, set the correct account in the installer.
  • No error handling. Unhandled exceptions in a service can crash it silently; wrap work in try/catch and log to the Event Log or a file.

Tips

  • Use a unique, descriptive ServiceName and a matching DisplayName so it is easy to find in services.msc.
  • Log start, stop, and errors to a file or the Windows Event Log during development; the service has no console output.
  • Test the service logic in a console app first, then move it into the service class to shorten debug cycles.
  • Set StartType to Manual while developing so it does not start on every boot.
  • Keep the service EXE in a stable folder (for example C:\Services\MyWorkerService) rather than bin\Debug, which you may clean.

Troubleshooting

  • Service installs but will not start. Check Event Viewer for the exception. Common causes: missing config file, wrong working directory, or an exception thrown in OnStart. Add file logging at the very top of OnStart to confirm it runs.
  • “The service did not respond to the start or control request in a timely fashion.” OnStart is taking too long. Move work to a background thread and return immediately.
  • InstallUtil reports “No public installers with the RunInstallerAttribute.Yes attribute could be found.” The installer class is missing or not public. Re-add the installer via Add Installer and ensure the class is public.
  • Access denied during install. Run the command prompt as Administrator.
  • Service starts then stops immediately. An unhandled exception occurred. Check Event Viewer and add try/catch with logging.
  • Cannot debug. Attach Visual Studio to the service process, or temporarily run the logic as a console app. Debugger.Break() can help if you have rights.

Frequently Asked Questions

Can I create a Windows Service without Visual Studio?

Yes, you can compile with csc.exe from the .NET Framework folder and install with InstallUtil.exe or sc.exe, but Visual Studio provides the template and installer designer that make it much easier.

What account should the service run under?

Use LocalSystem only if the service needs broad local privileges. For network access, NetworkService or a dedicated user account with minimal rights is safer. Set this in the service process installer.

How do I debug a Windows Service?

Attach Visual Studio to the running service process via Debug > Attach to Process, or add temporary logging to a file. Running the same code in a console app first is the fastest approach.

Can a Windows Service show a UI?

No. Services run in a non-interactive session and cannot show windows or message boxes reliably. Use logging, the Event Log, or a separate UI app that communicates with the service.

How do I uninstall the service?

Stop it first, then run InstallUtil.exe /u on the EXE or sc.exe delete <ServiceName> from an elevated prompt.

Does this work on .NET Core or .NET 5+?

The ServiceBase approach is specific to .NET Framework. Newer .NET versions use Microsoft.Extensions.Hosting.WindowsServices and a different project template, so the steps differ.