473,651 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Configuration Sections (app.config/web.config)

balabaster
797 Recognized Expert Contributor
Okay, I decided that I needed to understand the whole custom configuration file bits and so I've spent some time playing around with it. It seems (in the most) relatively straight forward. However, I've come unstuck with one scenario that I'm trying to figure out the code for:

Consider the following configuration:

[HTML]<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<section name="DummySect ion" type="ConsoleAp plication1.Dumm ySection, ConsoleApplicat ion1" />
</configSections>
<DummySection >
<DummyElement name="1" description="fi rst element">
<ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
<ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
<ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
<ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
</DummyElement>
<DummyElement name="2" description="se cond element">
<ElementData name="2.1" attr1="2.1.1" attr2="2.1.2" />
<ElementData name="2.2" attr1="2.2.1" attr2="2.2.2" />
<ElementData name="2.3" attr1="2.3.1" attr2="2.3.2" />
<ElementData name="2.4" attr1="2.4.1" attr2="2.4.2" />
</DummyElement>
<DummyElement name="3" description="th ird element">
<ElementData name="3.1" attr1="3.1.1" attr2="3.1.2" />
<ElementData name="3.2" attr1="3.2.1" attr2="3.2.2" />
<ElementData name="3.3" attr1="3.3.1" attr2="3.3.2" />
<ElementData name="3.4" attr1="3.4.1" attr2="3.4.2" />
</DummyElement>
</DummySection>
</configuration>[/HTML]

Perfect, I have a sample configuration file that can be used to configure my application, so now I've gotta create the classes that read it...

The code is kind of long winded...

I've got (for brevity I've cut the code down to just the bare bones):

Expand|Select|Wrap|Line Numbers
  1. Public Class ConfigClass : ConfigurationSection{
  2.   [ConfigurationCollection(GetType(DummyElementCollection), AddItemName:="DummyElement"]
  3.   DummyElement Elements{
  4.     get { return (DummyElementCollection)mybase("DummyElement"); }
  5. }
  6.  
  7. //Collections
  8. Public Class DummyCollection : ConfigurationElementCollection{
  9.   //Cut out code
  10. }
  11.  
  12. Public Class ElementDataCollection : ConfigurationElementCollection{
  13. }
  14.  
  15. //Configuration elements
  16. Public Class DummyElement : ConfigurationElement{
  17. }
  18.  
  19. Public Class ElementData : ConfigurationElement{
  20. }
Now, given that I have elements that are also collections, how do I account for this in my code? I've read many examples of custom sections using the standard syntax, i.e.:

[HTML]<MySection>
<MyCollection A>
<add key="A1" value="blahA1" />
<add key="A2" value="blahA2" />
</MyCollectionA>
<MyCollection B>
<add key="B1" value="blahB1" />
<add key="B2" value="blahB2" />
</MyCollectionB>
</MySection>[/HTML]

I can make all these work just fine... I know about the

[ConfigurationEl ementCollection (GetType(DummyE lementCollectio n)AddItemName:= "DummyEleme nt"]

and
[ConfigurationEl ementCollection (GetType(Elemen tDataCollection ), AddItemName:="E lementData"]

but where do I put these? Ahead of my class declarations for my collections or on the property where the collection is referenced? In both spots I get errors that one parameter or another is missing.

What happens when you have elements that have attributes and are also collections? The major difference I see between the config file I want to use and the standard one is that I'd like to have attributes in elements that are also headers for collections.

If I have attributes in the collection header, my code bombs telling me that there are unrecognized attributes.

Can anyone point me in the direction of decent documentation for this type of thing?
Jun 20 '08 #1
4 2690
int08h
28 New Member
You are very close to the solution, however it is not a very good practice in XML structure, because XML is a string semantic language, collection element should be presented in a collection wrap, my suggestion is:
Expand|Select|Wrap|Line Numbers
  1. <DummyElement name="1" description="first element">
  2.   <ElementData>
  3.     <ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
  4.     <ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
  5.     <ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
  6.     <ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
  7.   </ElementData>
  8. </DummyElement>
For your ElementData, you must implement a collection which inherits from ConfigurationEl ementCollection , let's call it ElementDataColl ection

Then you should make a property of ElementDataColl ection in your DummyElement class and apply below attribute to it
Expand|Select|Wrap|Line Numbers
  1. [ConfigurationProperty("ElementData")]
  2. [ConfigurationCollection(typeof(ElementDataCollection), AddItemName = "ElementData")]
Jun 22 '08 #2
balabaster
797 Recognized Expert Contributor
You are very close to the solution, however it is not a very good practice in XML structure, because XML is a string semantic language, collection element should be presented in a collection wrap, my suggestion is:
Expand|Select|Wrap|Line Numbers
  1. <DummyElement name="1" description="first element">
  2. <ElementData>
  3. <ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
  4. <ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
  5. <ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
  6. <ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
  7. </ElementData>
  8. </DummyElement>
For your ElementData, you must implement a collection which inherits from ConfigurationEl ementCollection , let's call it ElementDataColl ection

Then you should make a property of ElementDataColl ection in your DummyElement class and apply below attribute to it
Expand|Select|Wrap|Line Numbers
  1. [ConfigurationProperty("ElementData")]
  2. [ConfigurationCollection(typeof(ElementDataCollection), AddItemName = "ElementData")]
Yeah, I got an example working like that on Friday...and while I agree, syntactically it may be more um..."correct", it requires two extra classes that I personally think are unnecessary. I am currently using the "correct" method and was wondering how exactly I'd go about condensing my code.
Jun 23 '08 #3
balabaster
797 Recognized Expert Contributor
After having spent quite a few hours trawling for information regarding this subject and quite a few more deciphering it to figure out how to apply more complex configuration scenarios, I decided that the information available is poor at best. So I put together a demonstration solution for custom configuration and how to work with it.

I hope this is of help to people using custom configuration. If anyone can point out errors, omissions and areas for improvement, please yell.
Attached Files
File Type: zip ConfigurationExample.zip (68.5 KB, 172 views)
Jun 23 '08 #4
balabaster
797 Recognized Expert Contributor
I've also written a document to go with the demonstration code, to assist with interpretation. ..
Attached Files
File Type: zip CustomConfigurationSections.zip (20.3 KB, 157 views)
Jun 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3452
by: kathy | last post by:
What Application Configuration File is? And when/where/how to use it? Any documents on that? MSDN has somrthings scatter everywhere - hard to follow. thanks,
5
2274
by: rdcpro | last post by:
In reading MSDN docs on creating custom Configuration sections, I found this page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfcustomelementfornamevaluesectionhandlerdictionarysectionhandler.asp At the very bottom, it says: Configuration File This element can be used in the application configuration file, machine configuration file (Machine.config), and Web.config files that are not at the
3
2809
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on servicing a number of different web service operations from a single application, and because it's all XML, I've been scoping everything to a specific namespace targeting a specific application (these are all InfoPath forms). It seems that the...
1
1603
by: Paloma García | last post by:
Dear all, I have created personalized configuration sections in my web project following the instructions described in this page http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconDeclaringCustomConfigurationSections.asp The problem is that I using the same code and I get the error "Object reference not set to an instance of an object" in the line where I try to read the first parameter (in the page...
6
3795
by: Tabi | last post by:
Hi, I want to create a custom section in my web.config that can hold my custom values. I created a section in web.config as written below. <configSections> <section name="myCustomSection" type="ComIT.Applications.Common.ConfigurationSections.MyHandler" allowLocation="true" allowDefinition="Everywhere" /> </configSections>
3
5616
by: Jason Richmeier | last post by:
Is it possible to create a custom configuration file (other than app.config) that can be processed by classes in the System.Configuration namespace? I have seen a ton of articles that talk about custom sections (inside the app.config file) but I would like to make an entirely new config file. I opened a custom config file with the ConfigurationManager class but it seemd as if it ignored all of the content in the config file. Is there...
0
1342
by: Alex Sadomov | last post by:
Hi all I have a custom configuration section in the App.config file of the console application: --------------------------------------------------------------------------- <configuration> <configSections> <section name="MySection" type="..."/> </configSections> <MySection>
6
4800
by: Jeff Hegedus | last post by:
I have a dll that requires some configuration data. I put the configuration data in a custom configuration section in a config file that is loaded in the installation folder of the dll. If I install the dll to the same directory as the exe which uses it, everything works as expected but if I load the dll in any other directory, I get the following error... 2007-02-04 16:36:51,171 DEBUG CentrifugeBHO.CentrifugeBHO - dllPath =...
2
5106
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.