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'. . 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.
- Right click on the solution node --> Add --> New Project.
- '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. . 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.
- 'Add Project Output Group' wizard will appear on you. Select 'CodeCallService' from the combobox at the op on the wizard and click OK button. . 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.
- Select 'Application Folder' from the combobox (or list -view) and then select 'Primary Output form CodeCallSerice (Active)' item. Click OK button.
- Right click 'CodeCallServiceInstaller' project node on the solution explorer --> Rebuild
- Right click 'CodeCallServiceInstaller' project node on the solution explorer --> Install. . 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.
- 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.
- Select the service, right click on it and you will see pause, stop and other options.
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.
- 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. . 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.
- 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 Files\ReliSource Technologies Ltd\CodeCallServiceInstaller'. Note that, you need to make sure that the service is running.
And that's all for this tutorial. Post your problems/feedback/suggestions/ in this thread if there is any.
Note: The attachment CodeCall.zip file contains a visual studio solution for both service and setup projects.