Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Windows Service -- Part 2: Installing & Debugging Windows Service

Windows Service Installing Service Debugging Service C# Service Project Attach to Process Setup Project Deployment Project combobox

  • Please log in to reply
No replies to this topic

#1 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 05 June 2012 - 04:00 AM

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'.ser_insta_com.PNG . 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.serviceProcessInstaller.PNG
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. depl_proj.PNG
  • '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.Depl_Add_Proj.PNG . 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_out.PNG
  • 'Add Project Output Group' wizard will appear on you. Select 'CodeCallService' from the combobox at the op on the wizard and click OK button.select_proj.PNG . 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. cust_Act.PNG
  • Select 'Application Folder' from the combobox (or list -view) and then select 'Primary Output form CodeCallSerice (Active)' item. Click OK button.cust_Act2.PNG
  • 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.installing.PNG . 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.installing2.PNG
  • 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.scm.PNG
  • Select the service, right click on it and you will see pause, stop and other options.scm2.PNG

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.debug.PNG
  • 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.attach_process.PNG . 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.debug2.PNG
  • 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.debug3.PNG

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.

Attached Files

  • 0

Also tagged with one or more of these keywords: Windows Service, Installing Service, Debugging Service, C# Service Project, Attach to Process, Setup Project, Deployment Project, combobox

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download