Through out this series of tutorials we’ll learn how to create a Windows service project in C#, how to install it, how to debug an installed Windows service and how control a windows service from an application. This is a three series tutorials on Windows service. In the first part of this ‘Window Service’ tutorial series, we learned about how to create a Windows service project. In this part we’ll learn how to install the service and how to debug it. In the last part we’ll learn how to control a service from another client application. Here ‘controlling a service’ means starting, stopping, sending commands to a service.

The job of the service will be to monitor the directory of the service’s exe for any file/folder creation during the service running. The service will keep a list of paths of items (file/folder) that will be created in the service’s exe directory.

Adding Installer Components and Deployment Project
First, we need to create service and process installer component which will install our service and service process in the system. To create them follow the steps…

  • Double click the ‘FileWatcherService.cs’ component on the ‘Solution Explorer’. The component will be viewed in designer mode.
  • Right click on the designer –> Add Installer. “ProjectInstaller.cs” component will be added in our project. The component contains two sub components — ‘ServiceInstaller’ & ‘ServiceProcessInstaller’.Attached File  ser_insta_com.PNG   47.59K   14 downloads. These are the two components that will install our service and our service process to the Windows system.
  • Double click the ‘ProjectInstaller.cs’ component from solution explorer and it will open in designer. Right click the ‘serviceProjectInstaller1’ component –> Properties. Change the ‘Account’ property value to ‘LocalService’ from the values combobox.Attached File  serviceProcessInstaller.PNG   42.22K   15 downloads

Then we need to create a setup project which will create a MSI installer deployment package for our service. To create a deployment project, follow the steps…

  • Right click on the solution node –> Add –> New Project. Attached File  depl_proj.PNG   51.06K   14 downloads
  • ‘Add New Project’ wizard will appear on you. Select ‘Other Project Types–>Setup and Deployment’ from the ‘Project Types’ options (left side on the wizard). Select ‘Setup Project’ template from the ‘Templates’ options (right side on the wizard). Give a name to our setup project — let us name it ‘CodeCallServiceInstaller’. And click on Ok button.Attached File  Depl_Add_Proj.PNG   33.43K   14 downloads. A setup project will be created and added into our solution.
  • Right click on the ‘CodeCallServiceInstaller’ project node in the solution explorer –> Add –> Project Output.Attached File  add_project_out.PNG   48.43K   13 downloads
  • ‘Add Project Output Group’ wizard will appear on you. Select ‘CodeCallService’ from the combobox at the op on the wizard and click OK button.Attached File  select_proj.PNG   49.24K   14 downloads. Now our service is added into the setup project to setup.
  • Right click on the ‘CodeCallServiceInstaller’ project node in the solution explorer –> View –> Custom Actions. Custom actions editor will be opened.
  • Right click ‘Custom Actions’ node in the editor –> ‘Add Custom Action’. ‘Select Item in Project’ wizard will appear on you. Attached File  cust_Act.PNG   55.29K   17 downloads
  • Select ‘Application Folder’ from the combobox (or list -view) and then select ‘Primary Output form CodeCallSerice (Active)’ item. Click OK button.Attached File  cust_Act2.PNG   49.76K   15 downloads
  • Right click ‘CodeCallServiceInstaller’ project node on the solution explorer –> Rebuild

Now it is time to install and run our service to the Windows system. To install and run the service, follow the steps…

  • Right click ‘CodeCallServiceInstaller’ project node on the solution explorer –> Install.Attached File  installing.PNG   134.86K   14 downloads. We can also install it from the output (CodeCallServiceInstaller.MSI or setup.exe) of the project ‘CodeCallServiceInstaller’. Actually we need to provide these outputs of our setup project to our clients so that then can install it on their machines.Attached File  installing2.PNG   40.73K  16 downloads
  • Go to Windows Start Menu –> Run. Type services.msc and press Enter key. ‘Windows Service Control’ will appear on you. Try to find our service — CCFSWatcher. After selecting it, right click on it –> Start. And our service will start and the ‘onstart’ method will be called.Attached File  scm.PNG   33.82K   11 downloads
  • Select the service, right click on it and you will see pause, stop and other options.Attached File  scm2.PNG   35.88K   10 downloads

Debugging Windows Service
To debug from visual studio, the service project must be build in ‘Debug’ mode and the debug build must need to be installed. When I was following the above steps, my project was in ‘Debug’ configuration. To debug our CCFSWatcher service, follow the steps below.

  • Make sure that our CCFSWatcher is running.
  • Set breakpoints in ‘onpause’, ‘OnContinue’, ‘onstop’,  and in ‘_fsWatcher_Created’ event handler.Attached File  debug.PNG   20.73K   11 downloads
  • In Visual Studio, click ‘Debug’ menu –> ‘Attach to Process…’. ‘Attach to Process’ window will appear on you. Find and select ‘CodeCallService.exe’ from the process list and click on ‘Attach’ button.Attached File  attach_process.PNG   33.18K   11 downloads. After clicking the ‘Attach’ button, the visual studio will start debugging our service project.
  • To get a proof, let us pause our service from the ‘Windows Service Control’ window and you will see the the onpause method get hitted in visual studio.Attached File  debug2.PNG   53.83K   10 downloads
  • To debug our ‘_fsWatcher_Created’ create an empty text (or any type of) file in the directory where the service process installed. In my case the CodeCallService.exe path is ‘C:Program FilesReliSource Technologies LtdCodeCallServiceInstaller’. Note that, you need to make sure that the service is running

What Do You Think on This ? Say Here