I’ve been working on a basic rules based web service for InfoPath forms. The basic idea is to allow us to do “poor man’s integration.” to some back end systems now and then swap that out later without modifying the forms — since the forms will be adapted by several people and there may be more forms than we want to go back through. So a single web service that all InfoPath forms call on submission — by the way one they call in addition to whatever else they want to do — for instance, post themselves to a SharePoint site.
Anyway, so I needed a rules section in the config file where each rule had some attributes. The problem is that .NET isn’t setup to easily hand you back the XML from a section in the configuration file — actually it gets pretty upset about the section even being in the file. The solution is a IConfigurationSectionHandler. Here’s what it takes to implement…
1) Modify the web.config file.
a) Add a <configSections> tag. Note this must be the first tag in the <configuration> node. This looks something like…
<configSections>
<sectionGroup name=”customConfig”>
<section name=”rules” type=”InfoPathRouting.Classes.XMLConfigurationSectionHandler, infopathrouting” />
</sectionGroup>
</configSections>In this case, I’m creating a new section called customConfig and adding a section in it called rules, which my class (InfoPathRouting.Classes.XMLConfigurationSectionHandler) in my DLL will handle. (InfoPathRouting is the ASP.NET project name)
b) Add the actual section in the file. In this case I’d add something like:
<customConfig>
<rules>
<!– Whatever I want in here —>
</rules>
2) Add the class to the project. Here’s the XMLConfigurationSectionHandler that I wrote. The only restriction is that it must support the IConfigurationSectionHandler interface. That interface only defines one method, Create().
using System;
using System.Configuration;
using System.Xml;
namespace InfoPathRouting.Classes
{
public class XMLConfigurationSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
return (section);
}
}
}
In this case I wanted the XMLNode back so I could do my own thing with the XML fragment, so that’s what I returned.
3) Reference the configuration section in your code. The following line of code fetches the XMLNode for my new rules section:
XmlNode rules = (XmlNode) System.Configuration.ConfigurationSettings.GetConfig(“customConfig/rules”);
That’s it. I now have a repeatable way to store any kind of XML configuration I want in my app.config and web.config files.
No comment yet, add your voice below!