Articles → .NET WINDOWS SERVICE → Basic Example Of Windows Service In .Net
Basic Example Of Windows Service In .Net
Objective Of This Tutorial
Steps Involved
- Create a window service
- Modify OnStart() method of windows service(if required)
- Add the Service Installer to the windows service
- Use ‘installutil.exe’ in the command line to install the service.
Step 1: Create A Window Service
- First of all open visual studio and create a new project (New – Project – select ‘Windows Service’)
- A windows service application is created as shown below
Click to Enlarge
Step 2: Modify Onstart() Method Of Windows Service(If Required)
- Dispose - clean up any managed and unmanaged resources
- OnStart - control the service startup
- OnStop - control the service stoppage
Step 3: Add The Service Installer To The Windows Service
- Right click on ‘Service.cs’, click on ‘View Designer’ to open the designer view.
- In designer right click to open context menu.
- Now click on ‘Add Installer’.
- ‘ProjectInstaller.cs’ is added in solution explorer.
Click to Enlarge
Step 4: Use ‘Installutil.Exe’ On The Command Line To Install The Service
- Open the command prompt of Visual Studio.
- Go to the directory where the ‘WindowsService1.exe’ is located. In this case it is ‘bin/debug’ folder of the project.
- Run ‘Installutil WindowsService1.exe’ command to install the service.
Click to Enlarge
- Go to ‘Start’.
- Click on ‘Run’(or you can use windows + R shortcut)
- Type ‘services.msc’
- Click ‘OK'.
Click to Enlarge