Using Application Configuration Files in .NET
Welcome to the next installment of the .NET Nuts & Bolts column. This article covers using application configuration files in Microsoft .NET. It briefly outlines the concept of application configuration files and touches on the native support the Microsoft .NET Framework provides. In particular, it discusses using the System.Configuration namespace among others within the Framework.
Application Configuration Background
INI files generally worked great, except for the following drawbacks:
- Everyone wrote their own parsers for dealing with them.
- You had to have them on the machine where the application was running.
- They required disk operations to read their files.
System.Configuration Basics
appSettings Section
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ApplicationTitle" value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;Integrated
Security=false;User Id=sa;Password=;" />
</appSettings>
</configuration>
// Read appSettings
string title = ConfigurationSettings.AppSettings["ApplicationTitle"];
string connectString =
ConfigurationSettings.AppSettings["ConnectionString"];
Customized Configuration Sections
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection ApplicationTitle="Sample Console Application"
ConnectionString="Server=localhost;Database=Northwind;
Integrated Security=false;User Id=sa;
Password=;" />
<appSettings>
<add key="ApplicationTitle"
value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;
Integrated Security=false;User Id=sa;Password=;" />
</appSettings>
</configuration>
// Read customSection
System.Collections.IDictionary sampleTable = (IDictionary)
ConfigurationSettings.GetConfig("sampleSection");
string title = (string)sampleTable["ApplicationTitle"];
string connectString = (string)sampleTable["ConnectionString"];
Customized Configuration Sections and Groups
The issue with custom configuration sections alone is that they rely on attributes to store all of the settings as a part of a single element. This can become cumbersome when a large number of configurations are contained within a section or additional related sections. You can further organize similar custom sections into a section group, which makes it easier to work with numerous configurations and helps eliminate naming conflicts with custom configuration sections. This is especially useful where your application contains reference to several other DLLs that read configuration information from the host applications configuration file. A sectionGroup element is simply placed around one or more section elements to create a group. Expanding the example configuration file, the following configuration file contains a custom group, section, and the pre-defined appSettings:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
<sectionGroup name="CodeGuru">
<section name="utilitySection"
type="System.Configuration.NameValueSectionHandler,
System,Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<sampleSection ApplicationTitle="Sample Console Application"
ConnectionString="Server=localhost;Database=Northwind;
Integrated Security=false;User Id=sa;
Password=;" />
<CodeGuru>
<utilitySection>
<add key="ApplicationTitle"
value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;
Integrated Security=false;User Id=sa;
Password=;" />
</utilitySection>
</CodeGuru>
<appSettings>
<add key="ApplicationTitle"
value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;
Integrated Security=false;User Id=sa;Password=;" />
</appSettings>
</configuration>
// Read custom sectionGroup
NameValueCollection collection = (NameValueCollection)
ConfigurationSettings.GetConfig("CodeGuru/utilitySection");
string title = collection["ApplicationTitle"];
string connectString = collection["ConnectionString"];
Possible Enhancements
- Introduce some form of encryption to keep configuration settings safe. While the application configuration files are secured by operating system permissions, that won't always prevent prying eyes from seeing the information they contain. The use of plain text-based XML makes it insecure to store items such as connection string information that contain passwords within the configuration file. Building your own wrapper around the System.Configuration or creating a custom configuration section and implementing the IConfigurationSectionHandler interface in a custom handler allows you to build your own methods to include encryption to keep information secure in the application configuration file.
- Use application configuration settings to dynamically assign field and/or property values. This won't work for standalone DLLs that don't have a configuration file, but it will work if your DLL is utilized by an executable or ASP.NET application, as is often the case with utility libraries or business logic components.
No comments:
Post a Comment