How to read Custom Configuration Section

Topics: Building and extending application blocks, General discussion
Aug 16, 2007 at 12:59 PM
I have created a Custom Configuration Section like other blocks does. Following is the snap shot of XML section:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="dataFilesConfiguration" type="DataFiles.DataFilesSettings, DataFiles" />
</configSections>
<dataFilesConfiguration>
<dataFiles>
<add name="Price" treeIndex="1" fileDataType="DataFiles.PriceFileDataSettings, DataFiles" filePath="" aplFilePath="text" type="">
<dataElements>
<add name="SecId" elementDataType="DataFiles.DataElementSettings" readonly="true" defaultValue="0" visible="false" type="" />
<add name="PriceValue" elementDataType="DataFiles.DataElementSettings" readonly="false" defaultValue="0" visible="true" type="" />
</dataElements>
</add>

<add name="Accounts" treeIndex="2" fileDataType="DataFiles.FileDataSettings, DataFiles" filePath="" type="">
<dataElements>
<add name="AccountId" elementDataType="DataFiles.DataElementSettings" readonly="true" defaultValue="0" visible="false" type="" />
</dataElements>
</add>
</dataFiles>
</dataFilesConfiguration>
</configuration>

Also I have created DataConfigurationSettings class inherited from SerializableConfigurationSection and created properties and it works.

As you can see the bold words which are infact class names defined within this library for testing purpose.

What i want to do is read this configuration section like other application blocks do. This configuration could be in app.config file or in seperate file. So i can't decide either i use SystemConfigurationSource or FileConfigurationSource.

I try to use EnterpriseLibraryFactory.BuildUp<T>(IConfigurationSource) method to create factory for this section but fails.

Please guide me about how can i achieve this, any article webcast etc.

Thanks,
Zeeshan Gulzar
Aug 16, 2007 at 1:09 PM
Hi,

Can you describe the nature of the failure you get?

In any case, it's one thing to read configuration from a source and a different thing to build objects based on that configuration, and so far you've only stated that you want to read the configuration. You wouldn't use the EnterpriseLibraryFactory.BuildUp<T>(IConfigurationSource) method to do it; instead, you would do something like ConfigurationSourceFactory.Create().GetSection("dataFilesConfiguration").

The Application Block Software Factory will help you create providers, configuration objects and all the plumbing code to have an "EntLib ready" block, if that's what you need. If you just want to read configuration, the method described above should be enough.

Hope this helps,
Fernando