<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:cs="http://blogs.iis.net/"><channel><title>Dave&amp;#39;s Blog : Managed Code</title><link>http://blogs.iis.net/davcox/archive/tags/Managed+Code/default.aspx</link><description>Tags: Managed Code</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Accessing custom properties at runtime through managed modules (part 2)</title><link>http://blogs.iis.net/davcox/archive/2007/11/21/accessing-custom-properties-at-runtime-through-managed-modules-part-2.aspx</link><pubDate>Wed, 21 Nov 2007 19:53:00 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2019494</guid><dc:creator>davcox</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/davcox/rsscomments.aspx?PostID=2019494</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/davcox/commentapi.aspx?PostID=2019494</wfw:comment><comments>http://blogs.iis.net/davcox/archive/2007/11/21/accessing-custom-properties-at-runtime-through-managed-modules-part-2.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Ok, now I want to do something in my managed module during runtime that will only read from my custom configuration properties.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I need someplace to start ... like a starter kit!&amp;nbsp; :)&amp;nbsp; &lt;BR&gt;&lt;A href="http://www.iis.net/downloads/default.aspx?tabid=34&amp;amp;i=1302&amp;amp;g=6" mce_href="http://www.iis.net/downloads/default.aspx?tabid=34&amp;amp;i=1302&amp;amp;g=6"&gt;http://www.iis.net/downloads/default.aspx?tabid=34&amp;amp;i=1302&amp;amp;g=6&lt;/A&gt;&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;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&amp;nbsp;the IIS pipeline.&amp;nbsp; So I took a look at the ASP.net integration articles.&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;Finally, I found this article:&lt;BR&gt;&lt;A href="http://www.iis.net/articles/view.aspx/IIS7/Hosting-Web-Applications/ASP-NET/ASP-NET-Integration-with-IIS7?Page=3" mce_href="http://www.iis.net/articles/view.aspx/IIS7/Hosting-Web-Applications/ASP-NET/ASP-NET-Integration-with-IIS7?Page=3"&gt;http://www.iis.net/articles/view.aspx/IIS7/Hosting-Web-Applications/ASP-NET/ASP-NET-Integration-with-IIS7?Page=3&lt;/A&gt;&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;I have my modules working, the starter kit was very helpful to get my Visual studio project&amp;nbsp;setup properly.&amp;nbsp;&amp;nbsp;Athough it didn’t have a reference to&amp;nbsp;Microsoft.Web.Administration which may be a common issue; something to add for future starter kits.&amp;nbsp; This is an important issue that I realized later.&amp;nbsp; Most asp.net modules will be using System.Configuration which does not support the&amp;nbsp;custom properties you add through IIS and the custom schema we've described.&amp;nbsp; Instead, you must use Microsoft.Web.Administration to access this kind of configuration.&amp;nbsp; These two API's unfortunately don't always play well together, so you need to know when you're using which one and why.&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;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 … &lt;/P&gt;
&lt;P mce_keep="true"&gt;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!&amp;nbsp; Hmm, why is this?&amp;nbsp; 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).&amp;nbsp; Well, the permission's were set properly, both of them can read configuration.&amp;nbsp; But I didn't realize that ServerManager object (in Microsoft.Web.Administration) opens up config for write access, which is not allowed!&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;I have to use WebConfigurationManager instead of ServerManager …and luckily MSDN helps me: &lt;A href="http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx"&gt;http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx&lt;/A&gt;&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;And&amp;nbsp;I found out later that Carlos had just blogged about this!&amp;nbsp; but the search engines had not yet found this great blog post:&lt;BR&gt;&lt;A href="http://blogs.msdn.com/carlosag/archive/2007/09/28/IIS7ManagedConfigurationAPI.aspx" mce_href="http://blogs.msdn.com/carlosag/archive/2007/09/28/IIS7ManagedConfigurationAPI.aspx"&gt;http://blogs.msdn.com/carlosag/archive/2007/09/28/IIS7ManagedConfigurationAPI.aspx&lt;/A&gt;&amp;nbsp; This is where Carlos goes into some detail about the difference between using design-time and run-time API's (which he implemented).&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;Great!!&amp;nbsp; Now my module is working ... here is a code snippet:&lt;/P&gt;
&lt;P mce_keep="true"&gt;using System;&lt;BR&gt;using System.Collections.Generic;&lt;BR&gt;using System.IO;&lt;BR&gt;using System.Web;&lt;BR&gt;using System.Diagnostics;&lt;BR&gt;using Microsoft.Web.Administration;&lt;/P&gt;
&lt;P mce_keep="true"&gt;namespace MyIIS7Modules&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class PonyModule : IHttpModule&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Init(HttpApplication application)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Subscribe to the BeginRequest event &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; application.BeginRequest += new EventHandler(this.CheckForPony);&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Dispose()&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void CheckForPony(Object source, EventArgs e)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpApplication application = (HttpApplication)source;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpContext context = application.Context;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string HttpUrl = application.Request.Url.ToString().ToLower();&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// if this is a PONY request, then let's handle it, otherwise just continue&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (HttpUrl.Contains("pony"))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the custom configuration section from that file&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConfigurationSection section = WebConfigurationManager.GetSection("ENC-Pony");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool PonyAnswer = (bool)section.GetAttributeValue("CanIHaveAPonyPlease");&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; application.Response.StatusCode = 200;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (PonyAnswer)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ResponseEntity += ("&amp;lt;H1&amp;gt;" + section.GetAttributeValue("YesResponse") + "&amp;lt;/H1&amp;gt;&amp;lt;BR&amp;gt;");&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;At a later date, I'll post my entire module with a brief overview of it's capabilities and how to use it.&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;Dave &lt;/P&gt;&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2019494" width="1" height="1"&gt;</description><category domain="http://blogs.iis.net/davcox/archive/tags/IIS7/default.aspx">IIS7</category><category domain="http://blogs.iis.net/davcox/archive/tags/Config/default.aspx">Config</category><category domain="http://blogs.iis.net/davcox/archive/tags/Managed+Code/default.aspx">Managed Code</category></item></channel></rss>