How to Create a Custom Windows Service
In this article
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
- 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 withUseWindowsService(). - Add the service class. In the designer that opens, right-click and select Add Installer. This adds
serviceInstaller1andserviceProcessInstaller1. SetserviceInstaller1.ServiceNameto a unique name such asMyWindowsService, and setserviceProcessInstaller1.AccounttoLocalSystem(orNetworkServiceif the service needs network access with limited rights). - Write the service logic. Override
OnStartandOnStop. KeepOnStartshort: spawn a background thread or start a timer, then return. IfOnStartblocks, the Service Control Manager will time out and report error 1053. InOnStop, signal the worker to finish and wait briefly for it to exit. - Build the project. Use Build > Build Solution. The output lands in
bin\Debugorbin\Release. Copy the whole output folder to a permanent location such asC:\Services\MyWindowsService; do not run it from your user profile, because the service account may not have access there. - 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, runsc.exe create MyWindowsService binPath= "C:\Services\MyWindowsService\MyWindowsService.exe"instead. - Start the service. Open Services (press Win+R, type
services.msc, press Enter), find your service by name, right-click and choose Start. Alternatively, runsc.exe start MyWindowsServiceornet start MyWindowsServicefrom an elevated prompt. - 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 byEnvironment.UserInteractiveso you can run the executable as a console app during development. - Uninstall when needed. Stop the service first, then run
installutil.exe /u C:\Services\MyWindowsService\MyWindowsService.exe(orsc.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 fromAppDomain.CurrentDomain.BaseDirectory. - Forgetting to add an installer. Without
serviceInstaller1,installutilhas nothing to install and reports no installers in the assembly. - Running as the wrong account.
LocalSystemhas broad local rights but limited network access;NetworkServiceis 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
EventLogcomponent in .NET writes under Applications and Services Logs. - Set
CanStop,CanPauseAndContinue, andCanShutdowntotrueonly if you actually implement those overrides; otherwise leave themfalse. - Use
sc.exe qc MyWindowsServiceto 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 avoidsinstallutilentirely.
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.