Low-Cost High Availability: Simple Database Monitoring in a Windows Environment : Page 4
The .NET Framework 2.0 and Visual Studio 2005 make it easy to create a basic but extensible database-monitoring solution without a lot of complexity or coding.
by John Paul Cook
Sep 20, 2006
Page 4 of 5
Creating the Windows Service Monitoring Program
To create the actual Windows Service code, you need to open the TestMonitor project and copy code from TestMonitor.cs to a Windows Service project. Next, start Visual Studio 2005, choose your preferred programming language, and select Windows Service as the project type.
You'll want to override the default file name of Service1.cs or Service1.vb with a more meaningful name, such as Monitor.cs. Click the hyperlink that says click here to switch to code view or right-click the Monitor.cs file name and select View Code.
Now you're ready to create the Windows Service code:
As you did in the TestMonitor project, add a reference to the class library by using the Browse tab and select DbMonitorWrapper.dll. Add a C# using statement or VB Imports statement for DbMonitorWrapper.
Add a C# using statement or VB Imports statement for System.Timers.
Copy the code in the Main of the TestMonitor class, except for the Console.ReadLine, into the OnStart function in the Monitor class. Change the two occurrences of TestMonitor to Monitor.
Copy the entire monitorTimer_Elapsed event handler into the Monitor class.
Add a class-level instantiation of a Timer object:
public partial classMonitor : ServiceBase
Timer monitorTimer = newTimer();
protected override void OnStart(string args)
// TODO: Add code here to start your service.
Monitor test = newMonitor();
test.monitorTimer.Enabled = true;
test.monitorTimer.Interval = DbMonitor.IntervalMsec;
test.monitorTimer.Elapsed += new
private void monitorTimer_Elapsed(object sender,
protected override void OnStop()
// TODO: Add code here to perform any tear-down necessary to stop your service.
Add an installer class by right-clicking on the design surface of the Monitor class and selecting Add Installer (see Figure 1). If you would like to do further reading about installing services, see this MSDN article.
Figure 1. Adding an Installer to the Design View of Monitor
In the Design view for the ProjectInstaller, select serviceInstaller1 (or ServiceInstaller1 for VB). In the Properties window, set the ServiceName to Monitor and the StartType to Automatic (see Figure 2).
Figure 2. Setting serviceInstaller Properties in the Design View of the ProjectInstaller
Select the serviceProcessInstaller1 (or ServiceProcessInstaller1 for VB) on the Design view of the ProjectInstaller. Service execute in the context of a specific account. LocalService is more highly privileged than you need for this task, so you can rule it out. If you use Windows Authentication to connect to a remote database server, LocalSystem won't work because it presents anonymous credentials. You can use either NetworkService or a specific User account (see Figure 3). Because a User account requires additional configuration steps, it provides a comprehensive example.
Figure 3. Specifying a Service Account to Execute as a User Account
Right-click the project and select Properties. Click the Assembly Information button on the Application property page. Enter your company's name in the Company textbox. Add a description and click OK (see Figure 4).
Figure 4. Assembly Information Superimposed Over the Project's Properties
Add an application configuration file (see Figure 5). Its contents should match the contents of the application configuration file from the TestMonitor project.