Working with Windows Service using Visual Studio
This article explains the purpose of Windows Service. It also shows how to create Windows Service using Visual Studio 2008, Add installer and install it. It also tells how to install it using Command line. It also explains you to manage Windows Service using Computer Management like start, stop and pause the service.
Windows Service is a long running executable application that runs in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. Windows services can be configured to start when the operating system is booted and run in the background as long as Windows is running, or they can be started manually when required
Create a Windows Service
Follow the below steps to a create Windows Service
- Create a Windows Service project by picking Windows Service option from your Visual C# Windows projects.
- Enter your service name(Eg: WindowsService1) and click OK.
- This will create a Windows Service project and add Service1.cs class to the project.
- Switch to code view to start developing.
- Write the functionality that the service needs to do. In the above screen Service1.cs has two overridden functions OnStart and OnStop. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service.
- In this example, it writes a message into the event log when the service starts and stops. Code snippet is as below:
Protected override void OnStart(string args)
EventLog.WriteEntry("Service", "WindowsService1 Started",
Protected override void OnStop(string args)
EventLog.WriteEntry("Service", "WindowsService1 Stopped",
- Build the solution (Ctrl + Shift + B) and make sure that all the code doesn't have any bugs.
Create an Installer for your Windows Service
- From menu select File --> Add -->New project
- From Add New project, select Other Project Types and choose Setup Project
- Add our service to the installer. Right click the Application folder, Select Add --> Project output
- In Add Project Output Group widow, Select the project as our Windows service and select Primary Output and click OK.
- Add a project installer configuration code into your project. Right click from service class in your windows service project within design mode, and choose "Add Installer" from the menu, then ProjectInstaller class will create for you.
- Modify the service installer. Switch to code mode to start configure the behavior of your windows service application by selecting ProjectInstaller.Designer.cs
- Modify the account service will run as, the service startup type, the service name, etc.,.
Private void InitializeComponent()
this.serviceProcessInstaller1 = new
this.serviceInstaller1 = new
this.serviceProcessInstaller1.AfterInstall += new
this.serviceInstaller1.ServiceName = "Service1";
- Build the solution
- Build the installer from the setup project by right-click on the setup project , and choose build the project.
- Right click the setup project and choose Install
- Installing our windows service, as setup wizard will show up
Install and run the Service from Command Line
Windows service can be installed from command line without creating the installer. Follow the below steps to install windows service from command line.
- Build the windows service as it is bug free.
- Run the below command to register the windows service.
Installutil d:\documents\Visual Studio 2008\Projects\WindowsService1\bin\debug\WindowsService1.exe
- Run the below command to uninstall the windows service.
Installutil \u d:\documents\Visual Studio 2008\Projects\WindowsService1\bin\debug\WindowsService1.exe
Start and Stop the service
Go to Computer Management to start and stop the service. Follow the below steps:
- Right click on My Computer and choose Manage from the menu item.
- Under Services and Applications, you will see our service created.
- Right click our service and select Start or Stop.
The installed service can be managed by launching "Services" from the windows Control Panel --> Administrative Tools or by typing "Services.msc" in the run command. In Windows Vista and later, the services can be managed from the Services tab in Task Manager. The "Services" management console provides a brief description of the service functions and displays the path to the service executable, its current status, startup type, dependencies and the account under which the service is running. It enables users to
- Start, stop, pause or restart services.
- Specify service parameters.
- Change the startup type which includes Automatic, Manual and Disabled.
- Change the account under which the service logs on.
- Configure recovery options upon service failure.
- Export the list of services as a text file or a CSV file.
Allow Service to Interact with Desktop
The Windows Service does not have a user interface. But the developers can add the Form and other UI Components to the service. Follow the below steps to add user interface to the Windows Service:
- Open Service properties dialog box.
- Go to the Logon tab.
- Select the "Allow Service to Interact with Desktop" checkbox.
When the User Interface is added to the Windows Service, then it can be interacted by any logged in user. So it may lead to some security risk.
Security Context for Services
Services run in the context of the default system account, called LocalSystem, which gives them different access privileges to system resources than the user. You can change this behavior to specify a different user account under which your service should run. The account property for the process allows you to set the service to one of four Account types:
Follow the below steps to set the Account Types:
- After creating your service, add the necessary installers for it.
- In the designer, access the ProjectInstaller class and click the service process installer for the service you are working with.
- In the Properties window, set the Account to the appropriate value.
Debug Windows Service Applications
Debugging a service is not as straightforward as debugging other visual studio application types. To debug a service, we must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.
Follow the below steps to debug a service:
- Install your service.
- Start your service, either from Services Control Manager, Server Explorer, or from code.
- In Visual Studio, choose Processes from the Debug menu.
- The Processes dialog box appears.
- Click Show system processes.
- In the Available Processes section, click the process for your service, and then click Attach.
- The Attach to Process dialog box appears.
- Choose any appropriate options, and then click OK to close the dialog box.
- Set any breakpoints you want to use in your code.
- Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints.
Drawbacks of attaching a debugger
- Cannot debug the OnStart() or Main() method. Because the debugger can be attached to the service only after it has started running. By that time OnStart() method completes its execution and returned the control to the operating system.
- The debugger must be attached each time the project is started.
- This feature is not available for Visual Studio .Net Express Edition.
Log Information about Services
All Windows Service projects have the ability to interact with the Application event log and write information and exceptions to it. AutoLog property is used to enable this functionality in your application. By default, logging is turned on for any service that is created with the Windows Service project template