Visual Studio .NET makes it relatively simple to create a Windows Service.
- Start a new project
- Select Windows Service from the list of available project templates
- The designer will open in design mode
- Create instance of the System.Timers.Timer in the service class
- Set the Enabled property to True and the Interval property to 30000 milliseconds in OnStart event. Also invoke the Start method
- Same way set the Enabled property to False & invoke the Stop method in OnStop event
- Switch to the code behind view (F7) to add functionality to the service
Makeup of a Windows Service
In the code behind class you will notice that your Windows Service extends the System.ServiceProcess.Service class. All Windows Services built in .NET must extend this class. It requires your service to override the following methods which Visual Studio will include by default.
- Dispose - clean up any managed and unmanaged resources
- OnStart - control the service startup
- OnStop - control the service stoppage
Install the Windows Service
Windows Services are different than normal Windows based applications. It is not possible to run a Windows Service by simply running an EXE. The Windows Service should be installed by using the InstallUtil.exe provided with the .NET Framework or through a deployment project such as a Microsoft Installer (MSI) file.
Add an Installer
Having created the Windows Service will not be enough for the InstallUtil program to be able to install the service. You must also add an Installer to your Windows Service so that the InstallUtil, or any other installation program, knows what configuration settings to apply to your service.
- Switch to the design view for the service
- Right click and select Add Installer
- Switch to the design view of the ProjectInstaller that is added
- Set the properties of the serviceInstaller1 component
- ServiceName = My Sample Service
- StartType = Automatic
- Set the properties of the serviceProcessInstaller1 component
- Account = LocalSystem
- Build the Solution
Use InstallUtil to Install the Windows Service
Now that the service has been built you need to install it for use. The following instructions will guide you in installing your new service.
- Open a Visual Studio .NET Command Prompt
- Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode)
- Issue the command InstallUtil.exe MyWindowsService.exe to register the service and have it create the appropriate registry entries
- Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage
- In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services
- Start your service by right clicking on it and selecting Start
Each time you need to change your Windows Service it will require you to uninstall and reinstall the service. Prior to uninstalling the service it is a good idea to make sure you have the Services management console closed. If you do not you may have difficulty uninstalling and reinstalling the Windows Service. To uninstall the service simply reissue the same InstallUtil command used to register the service and add the /u command switch.
Debug the Windows Service
As with all other aspects, debugging a Windows Service is different than a normal application. More steps are required to debug a Windows Service. The service cannot be debugged by simply executing it in the development environment as you can with a normal application. The service must first be installed and started, which we covered in the previous section. Once it is started you attach Visual Studio to the running process in order to step through and debug the code. Remember, each change you make to the Windows Service will require you to uninstall and reinstall the service.
Attach to a Running Windows Service
Here are the directions for attaching to a Windows Service in order to debug the application. These instructions assume that you have already installed the Windows Service and it is currently running.
- Load the project into Visual Studio
- Click on the Debug menu
- Click on the Processes menu item
- Make sure the Show system processes is selected
- Locate your process in the Available Processes list based on the name of your executable and click on it
- Click the Attach button
- Click OK
- Click Close
- Set a break point in the timer1_Elapsed method and wait for it to execute