Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

email article
printer friendly

Build a Plug 'n' Play XML Windows Service
Get weather, stock, or news information and store the results as XML.
by Dan Wahlin

Posted December 3, 2002

Windows Services (formerly known as NT services) are processes that run behind the scenes on a Windows server or workstation. When set up properly, they can automatically start when a computer is rebooted without human intervention. As a result, they work well in situations where a long-running program must perform tasks on a regular basis.

I'll introduce you to a Timer Windows Service that lets you plug different .NET components into it automatically by simply dropping an assembly into the proper application folder. More specifically, the Timer Windows Service allows components that perform specific functionality to be executed on a timer. It is useful when you need to call a Web service on a frequent basis to get weather, stock, or news information or when you need to screen-scrape a Web page frequently to extract data. You could also use the Timer Windows Service to check an FTP site for a new flat-file or XML-document drop or for one of a million other tasks. The downloadable application includes three "pluggable" components that perform these 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 (using regular expressions) a Web page generated by a system-monitoring package and store the results as XML.

You can incorporate custom components into the Windows Service by adding an entry into the XML configuration file and then dropping the custom assembly into the Windows Service's application vfolder:

<ServicesConfiguration>
   <!-- key: Assembly name   
          value:Type (including namespace)-->
   <Services>
      <add key="EBCWeatherService.dll" 
        value="EBCServices.EBCWeatherService.
        WeatherService" />
      <add key="EBCStockQuoteService.dll" 
        value="EBCServices.EBCStockQuoteService.
        StockQuoteService" />
      <add key="EBCSiteScopeService.dll" 
        value="EBCServices.EBCSiteScopeService.
        SiteScopeService" />
   </Services>
   <!-- more configuration data follows -->
</ServicesConfiguration>


The Windows Service monitors changes in the configuration file and automatically adds new assemblies into its process without any restart (or reboot). This is possible because each custom component implements an interface named IService that defines two methods named StartTimer() and StopTimer(). When the configuration file is changed, the Windows Service automatically stops all component activities by calling StopTimer(), loads the appropriate types defined in the configuration file through 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, and more.

To try out the Timer Windows Service and its related components, download the MSI file, run it, go to Services in Control Panel, and start the service named EBCWindowsService. Download the complete code for the Timer Windows Service and its related components here. You'll of course need to have the .NET Framework installed for the service to run.

You also need to make sure you give the service write access to create the XML files containing the data retrieved from the Web services. If you go through a proxy server, add the necessary proxy information to the XML configuration file (named EBCWindowsService.exe.config) installed with the Windows Service (in the location 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 contacting me at . If your component provides a service that others can benefit from, I'll add it into the install package and post it to the XML for ASP.NET Developers Web site.

About the Author
Dan Wahlin authored XML for ASP.NET Developers (Sams) and founded Wahlin Consulting, which focuses on XML and Web Service consulting and training. Dan also operates the XML for ASP.NET Developers Web site: www.XMLforASP.NET. Find more information at http://www.xmlforasp.net/Dan.aspx.

Back to top












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home