|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article explains configuration section handlers defined in the What we can do with App.config?A configuration file can contain information that the application reads at run time. We can use configuration sections to specify this information in configuration files. The .NET Framework provides several predefined configuration sections and developers can also create custom configuration sections. Configuration sections have two parts: a configuration section declaration and the configuration settings. We can put configuration section declarations and configuration settings in the machine configuration file or in the application configuration file. At run time, section handlers, which are classes that implement the Note: It’s not a good practice to specify application settings in machine.config file. There are two types of configuration sections:
Predefined configuration Section (appSettings)The .NET Framework provides a predefined configuration section called <section name="appSettings"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
The sample declaration of the <appSettings file="appSettings.xml"/>
Application settings are defined in an external file, which should be in the application bin folder. A sample appSettings.xml is: <?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="article" value="Configuration Sections"/>
<add key="author" value="Palanisamy Veerasingam"/>
</appSettings>
or we can keep the <appSettings>
<add key="author" value="Palanisamy Veerasingam"/>
<add key="article" value="Configuration Sections"/>
</appSettings>
public string GetAuthor()
{
return (ConfigurationSettings.AppSettings["author"]);
}
User-Defined configuration sections or Custom configuration sectionsDeclaring custom configuration sections<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
The
We can specify the following values to this
Also, we can use <sectionGroup name="mainGroup">
<sectionGroup name="subGroup">
<section ...
</sectionGroup>
</sectionGroup>
I hope that by looking at the sample, you can understand the difference between these handlers. I not yet faced a situation to use Declaring user-defined section handlersTo declare a user-defined section handler, create a class by implementing the interface A sample class declaration is: public class MyConfigHandler:IconfigurationSectionHandler{…}
There is only one method defined in the interface object Create (object parent, object configContext, XmlNode section)
Sample public class MyConfigHandler:IConfigurationSectionHandler
{
public MyConfigHandler(){}
public object Create(object parent,
object configContext, System.Xml.XmlNode section)
{
CCompanyAddr objCompany=new CCompanyAddr();
objCompany.CompanyName = section.SelectSingleNode("companyName").InnerText;
objCompany.DoorNo = section.SelectSingleNode("doorNo").InnerText;
objCompany.Street = section.SelectSingleNode("street").InnerText;
objCompany.City = section.SelectSingleNode("city").InnerText;
objCompany.PostalCode =
Convert.ToInt64(section.SelectSingleNode("postalCode").InnerText);
objCompany.Country = section.SelectSingleNode("country").InnerText;
return objCompany;
}
}
I have declared a section in the App.config file as follows: <configSections>
...
<sectionGroup name="companyInfo">
<section name="companyAddress"
type="ConfigSections.MyConfigHandler,ConfigSections"/>
</sectionGroup>
</configSections>
Then the section is declared in App.config file as follows: <companyInfo>
<companyAddress>
<companyName>Axxonet Solutions India Pvt Ltd</companyName>
<doorNo>1301</doorNo>
<street>13th Cross, Indira Nagar, 2nd Stage</street>
<city>Bangalore</city>
<postalCode>560038</postalCode>
<country>India</country>
</companyAddress>
</companyInfo>
To read CCompanyAddr obj =
(CCompanyAddr)ConfigurationSettings.GetConfig("companyInfo/companyAddress");
ConclusionI have written a very simple application to understand these section handlers, so I haven’t commented my code. Feel free to reply.
|
||||||||||||||||||||||