MSDN Magazine Launch Issue - February 15, 2008 - (Page 47) types with additional information about what to replace and with what to replace it. To install this configuration section, you need to do two things. First, copy the responsemod_schema.xml file containing the section schema information to the %windir%\system32\inetsrv\config\schema directory. Then declare the configuration section in the server’s main configuration file, applicationHost.config. The latter task requires writing some code if you’d like to perform this installation programmatically. To simplify the process of installing IIS 7.0 configuration sections, I wrote the iisschema.exe tool, which performs both of these tasks automatically. You can get the tool from mvolo.com/blogs/serverside/archive/2007/08/04/IISSCHEMA.EXE-_2D00_-A-tool-to-register-IIS7-configuration-sections.aspx. With this tool, installing the configuration Figure 4 Strongly Typed Configuration Section Wrapper class ResponseModificationSection : ConfigurationSection { public bool Enabled { get { return (bool)this[“enabled”]; } set { this[“enabled”] = value; } } public ResponseModificationRuleCollection FilterRules { get { return (ResponseModificationRuleCollection) GetCollection(typeof(ResponseModificationRuleCollection)); } } } schema section becomes a single-step process: IisSchema.exe /install responsemod_schema.xml Now that the configuration section schema is installed and the section itself is declared in applicationHost.config, you can immediately begin using it to define configuration information for the module. You can use any of the IIS configuration tools or APIs to manage the configuration for this section, just like you would normally do for any of the built-in IIS 7.0 configuration sections. For example, I can enable the Response Modification feature for my application by using AppCmd, the IIS 7.0 command-line tool: %windir%\system32\inetsrv\AppCmd Set Config “Default Web Site/” /section:responseModification /enabled:true I can also add a rule to the rule collection of the responseModification configuration section: %windir%\system32\inetsrv\AppCmd Set Config “Default Web Site/” “/+[name=’StripWhitespace’,replaceType= ’Mvolo.ResponseModification.Filters.HtmlDeflate’]” If you then open the web.config file in the root of the default Web site (which is typically %windir%\inetpub\wwwroot), you will find the following code: with configuration encryption, and so on. However, in order for this configuration information to be useful, you need to be able to read it from the Response Modification module. The Microsoft.Web.Administration API is a new .NET Framework API provided in IIS 7.0 that enables programs to access the IIS 7.0 configuration information. In addition to allowing configuration and setup programs to manage configuration, it is also the API that can be used to read configuration information from within a managed module. You can use the API to read the configuration section in the module immediately after registering the configuration section because the API exposes a loosely typed model for reading configuration sections. Before you do this, you will need to add a reference to Microsoft.Web.Administration.dll, which is located in the %windir%\ system32\inetsrv directory. Note that this DLL is only included with IIS 7.0 on Windows Vista® or Windows Server® 2008 and is not included with the .NET Framework or Windows SDKs. With the assembly reference added, I can read the module’s configuration information: ConfigurationSection section = WebConfigurationManager.GetSection( context, “responseModification”); bool enabled = (bool)section[“enabled”]; If you ever modify the web.config file for the application to change the module’s configuration information and then upload it to the server, the application will be configured to use the new settings. Likewise, if you upload the application to another server that has the configuration section installed, you can be sure that the application will use the desired settings without needing to reconfigure it on that server. In addition to AppCmd, you can now use any of the IIS 7.0 configuration APIs to manage the configuration information for this section, including the managed Microsoft.Web.Administration API, the IIS 7.0 Windows Management Instrumentation (WMI) provider, the IIS 7.0 configuration COM objects, and Windows PowerShell®. In addition to setting and reading this configuration information, you can also perform any management task that is supported by the IIS 7.0 configuration system, including controlling how this section is delegated to applications, protecting its content The WebConfigurationManager class in the Microsoft.Web.Administration namespace is almost identical to WebConfigurationManager in the System.Web.Configuration namespace and is intended to replace the use of the latter for IIS 7.0 managed modules that read IIS 7.0 configuration. The GetSection method retrieves the configuration section object for the current HTTP request path. A Strongly Typed Configuration Class The ConfigurationSection object allows loosely typed access to any configuration section without any additional code. You can access the enabled attribute simply by retrieving it by name from the section object’s indexer and casting it to the expected type. However, you can take it one step further by generating a strongly typed wrapper for the configuration section. You can quickly generate the wrapper class by using the tool written by IIS 7.0 launch2008 47 http://mvolo.com/blogs/serverside/archive/2007/08/04/IISSCHEMA.EXE-_2D00_-A-tool-to-register-IIS7-configuration-sections.aspx http://mvolo.com/blogs/serverside/archive/2007/08/04/IISSCHEMA.EXE-_2D00_-A-tool-to-register-IIS7-configuration-sections.aspx http://mvolo.com/blogs/serverside/archive/2007/08/04/IISSCHEMA.EXE-_2D00_-A-tool-to-register-IIS7-configuration-sections.aspx
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.