Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET Configuration and Group Policy, Part 1: Creating and Using Custom Configuration Sections  : Page 4

This three-article series shows how to create and use custom configuration sections in ASP.NET application configuration files, and how to set Windows Group Policy at both the machine and domain level to control configuration settings from a central location.


advertisement
Using the Example Custom Configuration Section
To see the custom configuration section and handler in action, the downloadable code available with this article contains a web page that extracts and displays custom data from the Web.config file. Figure 2 shows the example application displaying values read from the Web.config file. This example also demonstrates the Group Policy Aware configuration handler that you will see described in a subsequent article.

 
Figure 2. Sample Application Display: The figure shows an ASP.NET application displaying values from the custom configuration section using the handler.
The code-behind file for Default.aspx contains a handler for the button Click event that displays the values exposed by the custom configuration section handler. The page references the CustomConfigSection namespace that contains the custom configuration handler classes, as well as the System.Web.Confguration namespace that contains the WebConfigurationManager class. Here's the code for the button Click event handler:

protected void btnGetConfig_Click( object sender, EventArgs e) { // Retrieve <CustomConnections> section from // configuration file CustomConfigSection.ConnectionSettingsSection configSection = (CustomConfigSection. ConnectionSettingsSection) WebConfigurationManager.GetSection("CustomConnections"); // Get value of "autoConnect" attribute on // Connections element lblAutoConnect.Text = configSection.AutoConnect.ToString(); // Get value of "deviceMode" attribute on // ConnectionItems element lblDeviceMode.Text = configSection.ConnectionItems.DeviceMode; // Iterate through collection of <add> elements within // ConnectionItems foreach (CustomConfigSection.ConnectionItemElement conn in configSection.ConnectionItems) { // Get value of "connectionType" and "price" attributes lblConnectionItems.Text += conn.ConnectionType + " = " + conn.Price.ToString() + " &nbsp; "; } // Get value of attributes on <defaultUser> element lblDefaultUser.Text = configSection.DefaultUser.DefaultUserName; lblUserLocation.Text = configSection.DefaultUser.DefaultUserLocation; }

The preceding code first obtains the custom section by calling the WebConfigurationManager class's GetSection method, specifying the section name CustomConnections. The entry in the <configSections> element in Web.config specifies that the handler for this section is the custom ConnectionSettingsSection class you saw in the previous sections of this article, and the code casts the returned configuration section to an instance of this type.

The .NET configuration system reads the custom configuration file section only when you call the GetSection method; therefore, if required, you can handle exceptions that the custom handler may raise when configuration errors occur.

Now the code can access the values exposed by the custom configuration handler, and display them in controls located within the page. Notice how easy it is to access values from the configuration, simply by referencing the properties exposed by the custom handler.

For example, the ConnectionItems property exposes the collection of ConnectionItemElement instances, each of which exposes the ConnectionType and Price properties. The DefaultUser property exposes an instance of the DefaultUserElement class, which itself exposes the DefaultUserName and DefaultUserLocation properties.

Increasing Manageability
Developers are increasingly pressured into building applications that are more manageable. It is an accepted fact that deployment, runtime, and maintenance costs make up over 80 percent of the lifetime cost of an application, and any techniques that can help to minimize these costs are extremely welcome.

Managing application configuration is a major part of the deployment and runtime tasks that administrators face, and best practice in application design and development tends toward central management of configuration data, together with the ability to impose specific configurations on users and groups of machines. In the ASP.NET arena, being able to control centrally configuration for multiple servers, such as a Web farm, reduces the cost and complexity of updating and managing Web.config files.

This series of articles increases application manageability by describing how to create custom configuration sections and providers that automatically verify the presence of configuration data matching a required schema, and how to expose that data through objects and properties that simplify consuming the data from your applications.

The next installment in this article series discusses creating and using Group Policy aware providers in ASP.NET.



Alex Homer is a director of Stonebroom, Ltd., a software development, consulting, and training organization. He was formerly lead technical author and reviewer for Wrox, specializing in Microsoft Web and database technologies. You can reach him through his Web site.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap