Welcome Guest!
Create Account | Login
Locator+ Code:

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

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Build Your First .NET Windows Service
Create a Windows Service application to provide monitoring for your .NET apps.
by Doug Thews

November 19, 2004

Technology Toolbox: VB.NET

Once limited to the domain of C++ and MFC programmers, Windows Services can now be used as a key part of your .NET applications' infrastructure. Much like moving database-level code into a database tier, you can implement a core set of application services (such as database connection pooling, event logging, and auditing) and deploy them as a Windows Service—available to all of your .NET applications without bloating them with redundant code. It's a good idea to implement common functionality across applications as a Windows Service for those features where an application requests a "service" (such as retrieving messages or getting a connection to a database). Otherwise, you might want to put common functionality code into a namespace in the global assembly cache (GAC).

ADVERTISEMENT

In this article, I'll introduce you to creating a Windows Service with VS.NET 2003. I'll show you how to create a simple monitoring service for all of your .NET desktop apps. For purposes of this article, I'll define monitoring as the ability to identify critical security events (such as gates opening and closing, fire alarms activating, sprinkler systems activating, and so on) across all security applications. The monitoring service will provide all applications with the ability to signal these events and the potential to log these events (outside of the Windows event log, which sometimes can be a security requirement). The service will also provide the ability for a desktop application to communicate with it.

A Windows Service is a process that runs unattended and without a user interface. Windows Services are available only in Windows NT, Windows 2000, Windows XP, and Windows Server 2003. Windows Services run in their own process space and can be configured to start up during the OS boot process. Other desktop applications can interact with them, if you configure them that way.

You can accomplish monitoring in two ways. First, you can have the service poll each application. Unfortunately, a lot of processor time is spent polling while no monitoring activity is happening. Second, you can use an event model, where each application registers with the monitoring service and then sends monitoring events as they happen. I'll show you how to use the event model in this article.

Create a Windows Service
Start by creating a Windows Service project. Select File | New Project, then select the Windows Service template (in this example, use AppMonitor as the service name). Notice that VS.NET creates a new project with a single VB.NET file called AppMonitor.vb. Double-click on this file to bring up a designer view (see Figure 1).

Notice that you can set several properties for the service project. For example, you can tell Windows that this service will respond to shutdown, pause, and continue, and other events. You can also tell Windows to log service events such as stop and start in the Windows Event Log by setting the AutoLog property to True. Figure 1 shows the Properties page for the AppMonitor service project.

Now take a look at the code for the service by clicking on the "click here to switch to code view" link. The first thing you'll notice is that the AppMonitor class inherits from the System.ServiceProcess.ServiceBase class. This base class holds all of the events and properties necessary to define and interact with the service. VS.NET 2003 generates code for the Main subroutine, which runs when the service first loads (not runs) during the OS boot process:

<MTAThread()> _
   Shared Sub Main()
   ' This is the main thread that gets 
   ' loaded when all the services
   ' listed in the service directory get 
   ' instantiated.
   Dim ServicesToRun() As _
      System.ServiceProcess.ServiceBase

   ' Setup the service to run
   ServicesToRun = New _
      System.ServiceProcess.ServiceBase() _
      {New AppMonitorService}
      System.ServiceProcess.ServiceBase.Run( _
      ServicesToRun)
End Sub



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