TechBuzzPro

How to Create a Custom Windows Service

· 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

You can create a custom Windows service by writing a small program that implements the service control protocol, installing it with the .NET Framework's InstallUtil tool or the sc.exe command, and then starting it from the Services console. The simplest path is a .NET Framework project in Visual Studio that inherits from ServiceBase, because the plumbing (start, stop, pause, shutdown) is handled for you. This guide walks through that approach and the manual alternative using sc.exe with any executable.

What You'll Need

  • A Windows PC with administrator rights (installing a service requires elevation).
  • Visual Studio (Community edition is fine) with the .NET desktop development workload, or the .NET SDK if you prefer command-line builds.
  • .NET Framework 4.x targeting pack, or .NET 6/7/8 with the Worker Service template if you want a modern cross-version service.
  • Basic familiarity with C# or another .NET language. If you are using a non-.NET executable, you only need the executable itself and sc.exe.
  • A folder for the compiled binaries that the service account can read (for example C:\Services\MyService).

Step-by-Step

  1. Create the project. In Visual Studio, choose Create a new project and pick Windows Service (.NET Framework). Name it something like MyWindowsService. If you are on .NET 6 or later, use the Worker Service template instead; it produces a console-style host that you convert to a Windows service with UseWindowsService().
  2. Add the service class. In the designer that opens, right-click and select Add Installer. This adds serviceInstaller1 and serviceProcessInstaller1. Set serviceInstaller1.ServiceName to a unique name such as MyWindowsService, and set serviceProcessInstaller1.Account to LocalSystem (or NetworkService if the service needs network access with limited rights).
  3. Write the service logic. Override OnStart and OnStop. Keep OnStart short: spawn a background thread or start a timer, then return. If OnStart blocks, the Service Control Manager will time out and report error 1053. In OnStop, signal the worker to finish and wait briefly for it to exit.
  4. Build the project. Use Build > Build Solution. The output lands in bin\Debug or bin\Release. Copy the whole output folder to a permanent location such as C:\Services\MyWindowsService; do not run it from your user profile, because the service account may not have access there.
  5. Install the service. Open an elevated Command Prompt (right-click Command Prompt > Run as administrator). Run installutil.exe C:\Services\MyWindowsService\MyWindowsService.exe. The tool is part of the .NET Framework and lives under %windir%\Microsoft.NET\Framework64\v4.0.30319; if it is not on your PATH, use the full path. For a Worker Service, run sc.exe create MyWindowsService binPath= "C:\Services\MyWindowsService\MyWindowsService.exe" instead.
  6. Start the service. Open Services (press Win+R, type services.msc, press Enter), find your service by name, right-click and choose Start. Alternatively, run sc.exe start MyWindowsService or net start MyWindowsService from an elevated prompt.
  7. Verify it runs. Check the service status in Services; it should read Running. If your service writes to a log file, confirm the file appears. For debugging, attach Visual Studio to the running process via Debug > Attach to Process, or add a Debugger.Break() call guarded by Environment.UserInteractive so you can run the executable as a console app during development.
  8. Uninstall when needed. Stop the service first, then run installutil.exe /u C:\Services\MyWindowsService\MyWindowsService.exe (or sc.exe delete MyWindowsService). Deleting a service does not remove its files; clean those up manually.

Common Mistakes

  • Doing work in OnStart. Long-running initialization makes the SCM think the service hung and returns error 1053. Start a worker and return immediately.
  • Using a relative path. The working directory for a service is C:\Windows\System32, not your app folder. Always use absolute paths or resolve them from AppDomain.CurrentDomain.BaseDirectory.
  • Forgetting to add an installer. Without serviceInstaller1, installutil has nothing to install and reports no installers in the assembly.
  • Running as the wrong account. LocalSystem has broad local rights but limited network access; NetworkService is the reverse. Pick the least privilege that works.
  • Installing from a network share or user folder. The service account may not be able to read the binaries, causing error 5 (access denied) on start.
  • Editing files in place while the service is running. File locks will block the copy. Stop the service first.

Tips

  • Give the service a unique, descriptive ServiceName; the display name can be friendlier and is what appears in Services.
  • Log to the Windows Event Log or a rolling file so you can diagnose failures without a debugger. The EventLog component in .NET writes under Applications and Services Logs.
  • Set CanStop, CanPauseAndContinue, and CanShutdown to true only if you actually implement those overrides; otherwise leave them false.
  • Use sc.exe qc MyWindowsService to inspect the configured binary path, start type, and account without opening the GUI.
  • For .NET 6+, the Worker Service template plus UseWindowsService() is the modern route and avoids installutil entirely.

Troubleshooting

If the service fails to start, check the Event Viewer under Windows Logs > Application for the error your code wrote. Error 1053 means OnStart took too long; move the work to a background thread. Error 5 means the account cannot read the binaries or a required folder; grant read and execute on the install folder. Error 1067 means the process exited unexpectedly; run the executable interactively as a console app to see the exception. If installutil reports that the assembly has no installers, confirm Add Installer was used and that the installer classes are public. If the service starts and immediately stops, check that OnStart does not throw and that any timers or threads are kept alive by a reference field.

Frequently Asked Questions

Can I create a Windows service without Visual Studio?

Yes. You can compile with csc.exe or the .NET SDK, then install with installutil or sc.exe. The service class and installer classes are the same; only the build tool changes.

Does a Windows service need a user logged in?

No. Services run in their own session under the configured account and start at boot if set to Automatic, independent of any interactive login.

How do I debug a service?

Run the executable directly as a console app during development by checking Environment.UserInteractive, or attach Visual Studio to the running service process via Debug > Attach to Process.

What is the difference between sc.exe and installutil?

installutil runs the installer classes in a .NET assembly and is the standard for .NET Framework services. sc.exe creates a service entry for any executable and is required for Worker Services and non-.NET binaries.

Can I run a service as a specific domain user?

Yes. Set serviceProcessInstaller1.Account to User and supply the username and password, or use sc.exe config MyWindowsService obj= "DOMAIN\user" password= "secret". The account needs Log on as a service rights.

Why does my service work in Debug but fail as a service?

Usually a path or permission problem. The service runs from System32 with a different account, so relative paths and user-profile folders break. Use absolute paths and grant the service account access.