Windows Services (formally known as NT services) are processes that run behind the scenes on a Windows server or workstation. When setup properly, they can automatically start when a computer is rebooted without any human intervention. As a result, they work well in situations where a long-running program must perform tasks on a regular basis.
This sample application includes a Timer Windows Service that allows different components to be plugged into it automatically by simply dropping a .NET assembly into the proper application folder. More specifically, the Windows Service allows components that perform specific functionality to be executed on a timer. It is useful when a Web Service needs to be called on a frequent basis to get weather, stock, or news information or when a Web page needs to be screen scraped frequently to extract data. It could also be used to check an FTP site for a new file drop or for one of a million other tasks. Currently the downloadable application includes three "pluggable" components that perform the following tasks:
- Call a Weather Web Service on a recurring basis and store the results as XML.
- Call a Stock Quote Web Service on a recurring basis and store the results as XML.
- Screen Scrape a Web page generated by a system monitoring package and store the results as XML.
Custom components can be incorporated into the Windows Service by adding an entry into a configuration file and then dropping the custom assembly into the Windows Service application folder. The Windows Service monitors changes in the configuration file and will automatically add new assemblies into its process without any restart (or reboot). This is possible because each custom component implements an Interface named IService which defines two methods named StartTimer() and StopTimer(). When the configuration file is changed, the Windows Service automatically stops all component timers by calling StopTimer(), loads the appropriate types that are defined in the config file via reflection, and then calls the StartTimer() method on each object.
The application supports calling remote servers and Web Services through proxies, variable timer settings for each component, automatic logging to the event log, custom XML configuration files, plus more. To try the Windows Service on your system, download the MSI file, install it, go to Services in the Control Panel, and start the service named EBCWindowsService. The complete code for the Timer Windows Service and its related components is included in the zip file (including a VS.NET solution with several different projects.)
You will need to make sure you give the service access to create the XML files containing the retrieved data. If you go through a proxy server you will also want to add the necessary proxy information to the configuration file (named EBCWindowsService.exe.config) installed with the Windows Service (in the location that you specify during the installation wizard). The file is full of comments explaining what each configuration setting does.
If you write different components implementing IService that can be plugged into the Timer Windows Service please let me know by sending a message to firstname.lastname@example.org. If they are something that others can benefit from I will add them into the install package and post them to the XML for ASP.NET Developers Website.
For those who are interested, this Windows Service application was written to replace an application I wrote several years ago in VB and Excel (http://www.asptoday.com/content.asp?id=257) that accomplished the same end goal. Man have times changed!