Browse DevX
Sign up for e-mail newsletters from DevX


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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

  1. 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.
  2. Add a C# using statement or VB Imports statement for System.Timers.
  3. 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.
  4. Copy the entire monitorTimer_Elapsed event handler into the Monitor class.
  5. Add a class-level instantiation of a Timer object:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using DbMonitorWrapper; using System.Timers; namespace MonitorService { public partial class Monitor : ServiceBase { Timer monitorTimer = new Timer(); public Monitor() { InitializeComponent(); } protected override void OnStart(string[] args) { // TODO: Add code here to start your service. DbMonitor.InitializeList(); Monitor test = new Monitor(); test.monitorTimer.Enabled = true; test.monitorTimer.Interval = DbMonitor.IntervalMsec; test.monitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(test.monitorTimer_Elapsed); } private void monitorTimer_Elapsed(object sender, ElapsedEventArgs e) { DbMonitor.TestConnections(); } protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. } } }

  6. 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.

    Click to enlarge

    Figure 1. Adding an Installer to the Design View of Monitor
  7. 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).

    Click to enlarge

    Figure 2. Setting serviceInstaller Properties in the Design View of the ProjectInstaller
  8. 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.

    Click to enlarge

    Figure 3. Specifying a Service Account to Execute as a User Account
  9. 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).

    Click to enlarge

    Figure 4. Assembly Information Superimposed Over the Project's Properties
  10. Add an application configuration file (see Figure 5). Its contents should match the contents of the application configuration file from the TestMonitor project.

    Click to enlarge

    Figure 5. Application Configuration File
  11. Build the solution and resolve any errors.

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