Accessing custom properties at runtime through managed modules (part 2)

Ok, now I want to do something in my managed module during runtime that will only read from my custom configuration properties.

I need someplace to start ... like a starter kit!  :) 
http://www.iis.net/downloads/default.aspx?tabid=34&i=1302&g=6 

This helps me get started, but it doesn’t tell me about the other events to get callbacks on as my module runs ... I realized finally that an IIS managed module is really an ASP.net module as far as it's ability to interface with the IIS pipeline.  So I took a look at the ASP.net integration articles. 

Finally, I found this article:
http://www.iis.net/articles/view.aspx/IIS7/Hosting-Web-Applications/ASP-NET/ASP-NET-Integration-with-IIS7?Page=3 

I have my modules working, the starter kit was very helpful to get my Visual studio project setup properly.  Athough it didn’t have a reference to Microsoft.Web.Administration which may be a common issue; something to add for future starter kits.  This is an important issue that I realized later.  Most asp.net modules will be using System.Configuration which does not support the custom properties you add through IIS and the custom schema we've described.  Instead, you must use Microsoft.Web.Administration to access this kind of configuration.  These two API's unfortunately don't always play well together, so you need to know when you're using which one and why. 

Ok, now to deploy my module … seems hard to find a good example of doing this … probably some arcane appcmd command that would do it. I’ll copy and paste config for now … oops, the web.config default in the starter kit didn’t have preCondition="managedHandler" in there …

Ok, I copied my module to the test machine and am trying to make requests ... but it gives me an error saying that it cannot read configuration!  Hmm, why is this?  What would be different about using the same configuration code in a console (running as the Administrator) and in the IIS pipeline (running as Network Service).  Well, the permission's were set properly, both of them can read configuration.  But I didn't realize that ServerManager object (in Microsoft.Web.Administration) opens up config for write access, which is not allowed! 

I have to use WebConfigurationManager instead of ServerManager …and luckily MSDN helps me: http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx 

And I found out later that Carlos had just blogged about this!  but the search engines had not yet found this great blog post:
http://blogs.msdn.com/carlosag/archive/2007/09/28/IIS7ManagedConfigurationAPI.aspx  This is where Carlos goes into some detail about the difference between using design-time and run-time API's (which he implemented). 

Great!!  Now my module is working ... here is a code snippet:

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Diagnostics;
using Microsoft.Web.Administration;

namespace MyIIS7Modules
{
    public class PonyModule : IHttpModule
    {

        public void Init(HttpApplication application)
        {
            // Subscribe to the BeginRequest event
            //
            application.BeginRequest += new EventHandler(this.CheckForPony);

        }

        public void Dispose()    {    }


        public void CheckForPony(Object source, EventArgs e)
        {

            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            string HttpUrl = application.Request.Url.ToString().ToLower();

            /// if this is a PONY request, then let's handle it, otherwise just continue
            if (HttpUrl.Contains("pony"))
            {

                // Get the custom configuration section from that file
                ConfigurationSection section = WebConfigurationManager.GetSection("ENC-Pony");
                bool PonyAnswer = (bool)section.GetAttributeValue("CanIHaveAPonyPlease");

                application.Response.StatusCode = 200;

                if (PonyAnswer)
                {
                    ResponseEntity += ("<H1>" + section.GetAttributeValue("YesResponse") + "</H1><BR>");

 

At a later date, I'll post my entire module with a brief overview of it's capabilities and how to use it. 

Dave

No Comments