Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Monitoring a web site on the local server with a Windows Service - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on MonoTouch for iPhone, MonoDroid for Android, and Windows Azure.

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Monitoring a web site on the local server with a Windows Service

I wrote the code below to monitor a website that was running on a local web server.  The goal was to reset the web server whenever the WebRequest object recieved a 404 error from the web server.  If a 404 error is recieved, the service runs the command specified in the web.config file.  The service operates on a timer and runs at a period defined in the web.config file.  Personally, I don't think the application is great in that it only looks for a 404 error, but it did solve my problem.  

Why restart the web service?  In our situation, a 404 was indicitive of a problem that required a restart.  I wrote this for a server running an old COM+/Classic ASP application that we are having trouble with.  In your situation, you can do as you please.

Paul Glavich(can I praise him enough) mentioned that .NET 2.0 provides a health monitoring service, so if you are using a .NET 2.0 app, you might want to look into that as oppossed to this solution. 

Enjoy

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Net;

namespace WebMonitor
{
    public partial class WebMonitorService : ServiceBase
    {
        public WebMonitorService()
        {
            InitializeComponent();
            _strUri = System.Configuration.ConfigurationSettings.AppSettings["URLToWatch"];
            _iInterval = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["IntervalTimeFrame"]);
            _strCommandToRun = System.Configuration.ConfigurationSettings.AppSettings["CommandToRun"];
        }
        private int _iInterval;
        private string _strUri;
        private string _strCommandToRun;
        private Timer _TimerCheck;

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            _TimerCheck = new Timer();
            _TimerCheck.Interval = _iInterval;
            _TimerCheck.Enabled = true;
            _TimerCheck.Elapsed += new ElapsedEventHandler(_TimerCheck_Elapsed);
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            if (null != _TimerCheck)
            {
                _TimerCheck.Enabled = false;
                _TimerCheck.Dispose();
                _TimerCheck = null;
            }
        }
        private void _TimerCheck_Elapsed(Object sender, ElapsedEventArgs e)
        {
            int i = 0;
            System.Net.HttpWebRequest hwRequest = (HttpWebRequest)System.Net.WebRequest.Create(_strUri);
            System.Net.HttpWebResponse hwResponse;
            System.Net.WebHeaderCollection whColl;
            try
            {
                _TimerCheck.Enabled = false;
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
            }
            catch (System.Exception sysExc)
            {
                if (-1 != sysExc.Message.IndexOf("(404)"))
                {
                    System.Diagnostics.Process.Start(_strCommandToRun);
                    EventLog.WriteEntry("WebMonitor", "The following command was run.  Command:" + _strCommandToRun, EventLogEntryType.Information);
                }
                else
                {
                    EventLog.WriteEntry("WebMonitor", sysExc.Message, EventLogEntryType.Warning);
                }
            }
            finally
            {
                hwRequest = null;
                hwResponse = null;
                _TimerCheck.Enabled = true;
            }
        }
    }
}
 web.config file contents:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="IntervalTimeFrame" value="60000" />
        <add key="URLToWatch" value="http://localhost/cmsvd" />
        <add key="CommandToRun" value="iisreset.exe" />
    </appSettings>
</configuration>

Posted: Jul 20 2006, 07:27 AM by Wallym | with no comments
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)