[editor's editor's note: I made another minor change. local-name() is a function, Jef. A function. That's what I get for not having a seamless publishing mechanism for my code straight from VS.NET.]
[editor's note: I made a minor mod to the code below after posting it, changing the xpath to ignore IncludeTypes if they weren't provided, rather than looking for the child configuration node by index]
Craig Andera blogged at one point about the last configuration section handler he would ever write. I had a similar idea, I googled, found his, liked it, used it. A couple of additional features were desirable to me, and so I modified it as follows:
// no warranties expressed or implied :) using System; using System.Configuration; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace Your.Namespace.Here { public class XmlSerializerSectionHandler : IConfigurationSectionHandler { public XmlSerializerSectionHandler() {} public object Create( object parent, object configContext, System.Xml.XmlNode section) { string typename = section.Attributes["type"].Value; XmlNodeList list = section.SelectNodes("./IncludedTypes/Type/text()"); XmlNode configNode = section.SelectSingleNode("child::*[not(local-name() = 'IncludeTypes')]"); Type[] types = new Type[list.Count]; for(int i = 0; i < list.Count; i++) { types[i] = Type.GetType(list[i].Value, true); } Type t = Type.GetType ( typename ); XmlSerializer ser = new XmlSerializer ( t, new XmlAttributeOverrides(), types, new XmlRootAttribute(configNode.LocalName), null); return ser.Deserialize(new XmlNodeReader(configNode)); } } }
This version allows me to have a different name for the configuration object than the class name -
via new XmlRootAttribute(configNode.LocalName) - and allows me to provide custom types to support plug-in style functionality in the objects I am configuring. This lets me avoid the monstronsity that is XmlInclude (hate it hate it hate it hate it) while still having extensibility. I will try to provide a more detailed example shortly, but until then, here, at least, is a snippet of an example configuration xml:
<configuration>
<configSections>
<section name="SomeConfiguration" type="Your.Namespace.XmlSerializerSectionHandler, Your.Assembly"/>
</configSections><SomeConfiguration type='Your.Namespace.SomeConfiguration, Your.Assembly'>
<IncludedTypes>
<Type>Your.Namespace.ThisMessageSender, Your.Assembly</Type>
<Type>LandSafe.Messaging.ThatMessageReceiver, Your.Assembly</Type>
<Type></Type>
</IncludedTypes>
<NodeConfiguration
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
LogConfigurationFileName='Log.config'
ShareCounters='true'
InstanceName='Configuration Example'>
<Nodes>
<Node Verbose='false'>
<MessageHandler xsi:type='TransactionHandler'/>
<Receiver xsi:type='ThatMessageReceiver'>
<QueuePath>.\requests</QueuePath>
<MillisecondTimeout>30000</MillisecondTimeout>
</Receiver>
<Sender xsi:type='ThisMessageSender'>
<QueuePath>.\happy</QueuePath>
</Sender>
<ErrorSender xsi:type='ThisMessageSender'>
<QueuePath>.\errror</QueuePath>
</ErrorSender>
<ThreadCount>2</ThreadCount>
</Node>
</Nodes>
</NodeConfiguration>
</SomeConfiguration>
</configuration>
I hope that you can monkey around with it and infer proper use from that. A couple of nits -- Xml Serialization only supports abstract classes, it does not support interfaces. I do not try to take into account scoping classes to particular namespaces, or any such thing, though I am sure it isn't that difficult to get there. This hits the sweet spot for me.
(parenthetically speaking, (note the parentheses) Is it just me, or is DevelopMentor bleeding like a stuck pig? First the Don leaves. Then he steals many of the DM-meisters from the fold to work in the big house. Then I notice people like Aaron Skonnard going to Northface University (at least temporarily?), and Mike Woodring moving his .NET sample page back to his indiependent consulting site. Then I notice Barracuda while googling for a SharePoint 2004 implementation I was working on. Then Pluralsight kicks off, and dm folks move like fleas from a wet dog, Craig A. being the latest I have noticed. And not too long ago, the middleware co gave up on training and traded their courseware, etc to dm in exchange for content hosting. Related? Inquiring minds want to know.)
Posted by: Barny | 2004.07.09 at 12:45 AM
Posted by: haacked | 2004.07.09 at 01:02 AM
Posted by: TorstenR | 2004.07.09 at 02:28 AM
Posted by: Jef Newsom | 2004.07.09 at 07:11 AM
Posted by: Craig | 2004.07.09 at 07:33 AM
Posted by: Ionic | 2004.11.19 at 04:33 PM