Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Windows Services in .NET  : Page 2

When you need to run unattended applications that can monitor directories and log events, don't even think about writing a desktop application—write a Windows service instead.


advertisement
The .NET Framework's System.ServiceProcess.ServiceBase class encapsulates essentially all of the "plumbing" required to create and control service applications. By basing your application on this class, most of the difficult work is already done for you. Some versions of Visual Studio .NET (VS.NET) provide a Windows Service application template that creates the skeleton of your application. Note that Visual Basic Standard Edition and Visual C# Standard Edition do not include the Windows Service application template, so you'll have to start from scratch.

At a minimum , you must add code to two methods in your service class. The Main() method is the entry point for the service and handles instantiating and running the service. Assuming a service named MyService, the code would look like this:

Shared Sub Main() Dim ServicesToRun() As _ System.ServiceProcess.ServiceBase ServicesToRun = New _ System.ServiceProcess.ServiceBase() _ {New MyService()} System.ServiceProcess.ServiceBase.Run _ (ServicesToRun) End Sub

The other method you must code is OnStart(), which executes when the service is started. Code in this method sets your service in motion so it can perform the tasks for which it is designed. For example, if your service monitors disk activity, your OnStart() method would instantiate one or more FileSystemWatcher objects.

Note that running a service and starting a service are two different things.
Note that running a service and starting a service are two different things. Running a service makes it available but does not start it. Once available, a service can be started or stopped either automatically, manually, or programmatically. Depending on the setting of the ServiceBase object's CanPauseAndContinue property, a service may also be able to pause and continue.

Logging Information
Services typically have no user interface. One of the only ways that you can provide information about the service to administrators is by writing entries to a Windows event log. Thanks to the .NET Framework's EventLog class, the procedure is quite simple. First, you instantiate an EventLog instance—typically in your service's Main() method, providing the log name as an argument to the class constructor. You must also specify the Source property for the log—usually a string that identifies your application. For example:

Dim MyLog As New EventLog(LogName) MyLog.Source = SourceName

Then, at other locations in your service, you call the WriteEntry() method to write information to the log. For example:



MyLog.WriteEntry(LogMessage)

You can use the Windows Event Viewer applet to view event logs, or you can use the EventLog class in an application to read existing logs. Be aware that creating and writing to a log consumes processor time and system resources, so this technique is best reserved for recording service-related events that are both important and occasional. You would not want to use an event log to record events that happen frequently, as would be the case with a service that monitors disk activity. In this type of situation it would be preferable to maintain information internally within the service and access it as needed..



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date