Browse DevX
Sign up for e-mail newsletters from DevX


Configuration API Improvements in ASP.NET 2.0 : Page 3

By introducing a spate of new configuration-specific features, .NET 2.0 makes managing your deployed .NET applications a breeze.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Enumerating Stored Connection Strings
You're not limited to interacting with single entries in web.config sections; you can enumerate the contents of any specific section in a configuration file. The following example shows you how to enumerate the built-in connectionStrings section by looping through all the connection strings stored in that section.

Like the previous examples, this example also uses the OpenWebConfiguration() method of the ConfigurationManager class to get a reference to the configuration element in the web.config file. Having done that, you can then easily get a reference to the connectionStrings section through the ConnectionStrings property of the Configuration object. The following example uses a foreach loop, to enumerate all the connection strings in that section and write them to a browser one at a time.

<%@ Import Namespace="System.Configuration" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat=server language=C# > public void Page_Load(object source, EventArgs e) { Configuration config = ConfigurationManager.OpenWebConfiguration( Request.ApplicationPath, "Configuration"); //Enumerate the connection strings //and display them Response.Write("<b>Connection " + "strings in Web.config:</b><br>"); foreach (ConnectionStringSettings connection in config.ConnectionStrings.ConnectionStrings) { Response.Write("Name: " + connection.Name + "<br>"); Response.Write("Connection String: " + connection.ConnectionString + "<br><br>"); } } </script> <html> <head> <title>Enumerating Configuration Settings</title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>

Figure 2 Enumerating Connection Strings: The figure shows the result of enumerating all the connection strings in a web.config file and writing them to a browser.
When you navigate to the page created by that code from a browser you'll see the output shown in Figure 2.

Adding Custom Configuration Sections
There are times where you may have to programmatically create a custom section and add it to a configuration file at runtime. For example, modifying configuration files programmatically is useful if you are creating a Windows installer for deploying a Web application and you need to create sections of the web.config file dynamically. You can do this by going through the following steps:
  • Create a custom class that derives from the ConfigurationSection class. This class represents the custom section you want to add to the configuration file.
  • Populate that object with the right values.
  • Add the custom object to the ConfigurationSectionCollection using the Add() method.
  • Persist the configuration changes by calling the Save() method of the Configuration class.
The following code shows an implementation of a custom class that represents a custom section in the Configuration file.

using System.Configuration; public class CustomSection : ConfigurationSection { public CustomSection() { } [ConfigurationProperty("name", RequiredValue = true, DefaultValue = "")] public string Name { get { return (string)base["name"]; } set { base["name"] = value; } } }

As you can see the preceding code, implementing such a custom class is very simple and straightforward. The custom class inherits from the ConfigurationSection class and exposes a Name property decorated with the ConfigurationProperty attribute. The ConfigurationProperty class represents a configuration property, attribute, or child element contained within an element of a configuration file. You supply the name of the attribute as an argument (which is "name," in this example) to the constructor of the ConfigurationProperty class, as well as a Boolean value indicating whether the attribute is required and the default value for that attribute.

Now that you have seen the implementation of the custom class, Listing 2 demonstrates how to serialize the contents of a custom class to a web.config file.

Just like the previous examples, you start by obtaining a reference to the web.config file. Then you create an instance of the CustomSection class. If you want to replace any existing custom configuration sections, you can use the Clear() method, as the preceding code does, which erases all of the custom configuration sections currently in the file. To store the modifications to disk, invoke the Save() method. The rest of the code in Listing 2 verifies whether the newly added section is actually present in the configuration file by looping through all the sections using the AllKeys property. While looping through the collection, if it finds a section named "CustomSection," the code simply displays a message indicating that the section was found.

ASP.NET contains many architectural enhancements designed to improve the manageability of deployed applications. For example, now that you can programmatically modify a configuration file using native .NET class methods, you write interfaces that let administrators modify configuration files easily and safely. In addition, the new features let you create sophisticated installers that can do everything from creating configuration files at deployment time to encrypting connection strings at runtime.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date