ASP.NET Podcast Show #98 - Building an IIS7 HttpModule (video and audio)

Original URL: http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2007/08/01/asp-net-podcast-show-98-building-an-iis7-http-module.aspx 

Subscribe <-- What you really want.

Download WMV

Download M4V - IPod and Zune

Download MP3 - Audio only.

Show notes:

  • Windows Server 2008.
    • Visual C# Express.
    • Visual Web Developer Express.
  • Class Library in C#.
    • IHttpModule Interface.
    • Init, Dispose.
    • Begin/End Request Events.
    • Other Server Events.
    • Messaging?
  • Web.Config.
  • Example.
  • IIS Manager.
  • WebDev Server vs. IIS7 Service.
  • Error and how to fix it.

 Source Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Messaging;

namespace IISWatcher
{
    public class WatchRequests : IHttpModule
    {
        public void Init(System.Web.HttpApplication app)
        {
            app.BeginRequest += new EventHandler(app_BeginRequest);
            app.EndRequest += new EventHandler(app_EndRequest);
        }
        void app_EndRequest(object sender, EventArgs e)
        {
            //HttpApplication app = (HttpApplication)sender;
        }
        void app_BeginRequest(object sender, EventArgs e)
        {
            string strReturn = "\r\n";
            HttpApplication app = (HttpApplication)sender;
            string strAddress = app.Request.UserHostAddress;
            string strUrl = app.Request.Url.AbsoluteUri;
            string strQS = app.Request.QueryString.ToString();
            RequestInfo ri = new RequestInfo();
            System.Diagnostics.EventLog.WriteEntry("HttpModule",
                "IpAddress: " + strAddress + strReturn + "URL:" + strUrl);
            System.Messaging.MessageQueue msq = new MessageQueue(@".\private$\HttpModuleQueue");
            ri.AbsoluteUri = strUrl;
            ri.IPAddress = strAddress;
            ri.QueryString = strQS;
            msq.Send(ri);
        }

        public void Dispose()
        {
        }
    }
    public class RequestInfo
    {
        public string IPAddress;
        public string AbsoluteUri;
        public string QueryString;
    }
}

Web.config for IIS7:

<configuration>
...................
    <system.webServer>
        <modules>
            <add type="IISWatcher.WatchRequests" name="IIS7RequestWatcher"/>
        </modules>
    </system.webServer>
</configuration>
 

No Comments