Click here to Skip to main content
Licence 
First Posted 30 Jul 2002
Views 286,439
Downloads 2,691
Bookmarked 130 times

Background processing using a .NET windows service

By | 30 Jul 2002 | Article
A reusable service that does database work and sends e-mails

Introduction

In a previous .NET project at work, I was asked to write a background process that can wake up at specified time intervals, call certain stored procedures to process a list of items retrieved from the database, and then send an e-mail notification to the interested party for every item processed. I immediately thought of doing it using a windows service. However, I cannot use my XYNTService here because no C++ code is allowed in this project. So it gives me an excuse to write my first windows service in .NET.

I hate to write code that can only be used once, so I put some effort into making this program simple and flexible enough that other people might be able to use it later. The windows service executable is named NotificationService.exe. Here is what the program does:

  1. Every 15 minutes the service will wake up and do the following processing.
  2. It calls a stored procedure to retrieve a list of items that needs to be processed.
  3. For each each item in the list, it calls another stored procedure to process the item.
  4. Then it sends an e-mail to notify the interested party.
  5. It will pause 1 second after processing each item to avoid hogging the CPU.

The time intervals it sleeps/pauses within the processing loop, the names of the stored procedures called, the database connection string, and the e-mail address as well as other fields, are all defined in an XML configuration file. If you want to reuse this program, you need only to:

  1. Write two stored procedures, one to retrieve the list of items and the other to process each item.
  2. Modify the XML configuration file.
  3. Run the windows service.

Of course, you can modify the service itself to do more complicated or customer-specific work.

The configuration file

The file Notification.xml contains all the information needed by NotificationService.exe. It should be placed in the Xml sub folder of the current folder (where the executable resides). Here is a sample of the Notification.xml file.

<notificationservice>
    <sleep>15</sleep>
    <pause>1000</pause>
    <dbconnection>Provider=myProvider;Database=myDatabase;Server=myServer;User
ID=myID;Password=</dbconnection>
    <getlistproc>GetList</getlistproc>
    <idfield>ID</idfield>
    <processitemproc>ProcessItem</processitemproc>
    <parametername>@ID</parametername>
    <email>
        <from>Sender@computer.domain.com</from>
        <to>Receiver@computer.domain.com</to>
        <cc></cc>
        <bcc></bcc>
        <subject>Notification Service</subject>
        <body>This is a notification</body>
    </email>
</notificationservice>

The <sleep> element is the number of minutes the program will sleep before waking up to retrieve and process the list of items. The <pause> element is the number of milliseconds the program will sleep after processing each item.

The <dbconnection> element is the database connection string. The <getlistproc> element contains the name of the stored procedure that retrieves the list of items from the database, the procedure returns a list of item identifiers (can be either strings or numbers). The <idfield> element is the field name of the previous stored procedure. The element <processitemproc> contains the name of the stored procedure to process each item, the procedure takes a parameter (the item identifier) whose name is in the <parametername> element.

The <email> element contains various fields used in the notification e-mail. If the <to> element is missing or empty, then no e-mail notification will be sent.

How the service works and how to install

When start up, the service will load the XML configuration file. Then it will spawn a worker thread that does the real processing job. When you stop the service, the worker thread will be killed.

The Working method is called by the worker thread to retrieve and process items. The XMLDB.dll described in my other CodeProject article is used to do all database work. Once you have the correct connection string defined in the XML configuration file, you will never have to worry about the database connection. Even if the database server is shutdown and later restarted, there is nothing that has to be done to the service because the component knows how to reconnect to the database automatically.

To install the program, you first unzip the 3 files NotificationService.exe, XMLDB.dll, and ServiceInstaller.exe into a folder on a machine that already has the .NET framework and copy the Notification.xml file to a sub folder named Xml. The following commands will install and start (run) the service (you need to change the file path to match the folder on your machine):

ServiceInstaller -i NotificationService c:\[...]\NotificationService.exe
ServiceInstaller -r NotificationService

The following commands will stop (kill) the service and un-install it:

ServiceInstaller -k NotificationService
ServiceInstaller -u NotificationService

The utility ServiceInstaller.exe is described in one of my other articles.

The implementation

The implementation is very simple. The class NotificationService is derived from System.ServiceProcess.ServiceBase. The OnStart and OnStop methods are overridden to create and destroy the worker thread. The Working method is called by the worker thread periodically to retrieve and process items from the database. The code is straightforward but it does demonstrate some basic programming techniques in the .NET environment. I hope at least one of you find this program useful. Check out my home page for other programs and articles.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBroken Links PinmemberGerhardKreuzer1:31 22 Oct '09  
Questionxml file corrupts on reload Pinmemberdeb P9:19 13 Mar '07  
QuestionHow to read from one stored procedure only PinmemberJonathan M Patrick11:44 28 Feb '07  
GeneralHi Xiangyang Liu Pinmembermpdesign6:32 15 Feb '07  
GeneralFailed to start PinmemberRichard T.19:42 25 Jun '06  
GeneralRe: Failed to start PinmemberHafeezi17:17 17 Aug '06  
QuestionCan I do this Pinmemberpmj.net18:12 15 May '06  
AnswerRe: Can I do this PinmemberXiangyang Liu20:57 15 May '06  
AnswerRe: Can I do this PinmemberAlexander Kloep21:16 17 May '06  
GeneralRe: Can I do this Pinmemberpmj.net3:26 19 May '06  
GeneralService showing in Process List. PinmemberBikash Rai18:56 9 Dec '05  
GeneralRe: Service showing in Process List. PinmemberXiangyang Liu20:36 9 Dec '05  
GeneralRe: Service showing in Process List. PinmemberBikash Rai22:30 9 Dec '05  
GeneralWindows Service Interface PinmemberNabeel Muhammed1:10 19 Sep '05  
Generalullasprabhu Pinsussullasprabhu10:14 26 Jan '05  
GeneralRe: ullasprabhu PinmemberXiangyang Liu16:29 26 Jan '05  
In that case, you need to debug the code. Here is what you need to do to debug the service:
 
1. Build the service in debug mode.
 
2. Install the service and start it.
 
3. From Visual Studio.NET, (a) set break point in the source code (at the place where the e-mail is about to be sent), (b) choose the "Debug" menu, (c) select "Processes...", (d) then select the service process from the list of processes and click the "Attach" button.
 
The execution of the code will stop when the break point is reached. You can step into the code and move forward one line at a time to figure out exactly where the problem is.
 
Good luck.
 

 




My articles and software tools





GeneralRe: ullasprabhu Pinsussullasprabhu13:00 28 Jan '05  
GeneralRe: ullasprabhu PinmemberXiangyang Liu6:31 29 Jan '05  
GeneralNot asking for help PinmemberJon Russell22:50 3 Nov '04  
GeneralWebforms Pinmemberwomble9995:30 3 Jun '04  
Generalhelp me Pinmemberjithus3:04 20 May '04  
GeneralRe: help me PinmemberXiangyang Liu3:39 20 May '04  
Generalthreadpool PinmemberPradeep K V4:31 19 May '04  
GeneralRe: threadpool PinmemberXiangyang Liu5:19 19 May '04  
GeneralRe: threadpool PinmemberPradeep K V5:35 19 May '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 31 Jul 2002
Article Copyright 2002 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid