Custom configuration section handlers with Namespaces

G

Guest

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 GetConfig method can only refer to a node by it's
local-name:

ConfigurationSettings.GetConfig("AccountsInRole");

What I would like to do is pass in a namespaceURI as well, so that I can use
the same node local-name for a number of applications, each with their own
section. Like:

ConfigurationSettings.GetConfig("AccountsInRole",
"urn:schemas-rdcpro-com:infopath/application1");

While I can specify a namespace in my ConfigurationSectionHandler, and use
namespace-qualified nodes, I don't see a way to specify that externally (ie,
from the calling web app):

public class XmlSerializerSectionHandler : IConfigurationSectionHandler
{

public object Create(object parent, object configContext, XmlNode section)
{
XPathNavigator xmlNav = section.CreateNavigator();
XmlRootAttribute xmlRoot = new XmlRootAttribute();
// wish I could explicitly set the Namespace I want!
xmlRoot.Namespace = xmlNav.NamespaceURI;
xmlRoot.ElementName = (string) xmlNav.LocalName;
string typeName = (string) xmlNav.Evaluate("string(@type)");
Type myType = Type.GetType(typeName);
XmlSerializer sr = new XmlSerializer(myType,xmlRoot);
// This will be cast to the appropriate type in the calling code.
return sr.Deserialize(new XmlNodeReader(section));
}
}


Of course, I could make my configuration a little more general, and have
nested custom configuration sections like:

<FormSubmitService>
<Application1>
<AccountsInRole type="ApplicationRoles.Accounts, ApplicationRoles">
<!-- config info for App 1 here -->
</AccountsInRole>
</Application1>
<Application2>
<!-- config info for App 2 here -->
</Application2>

and get the right one with:

ConfigurationSettings.GetConfig("FormSubmitService/Application1/AccountsInRole");

But it seems a shame to have XML Namespaces, and not be able to use them!
;^)

Am I missing something here? How would I get a particular "AccountsInRole"
using an XML namespace?

Regards,
Mike Sharp
 
S

Steven Cheng[MSFT]

Hi Mike,

Thanks for your posting. As for the namespace problem when fetching xml
config section in config file, this is caused by the internal search
mechanism of the .net 's System.Configuration.ConfigurationSettings class.
This class use a internal class called
System.Configuration.ConfigurationRecord to retreive the section specified
by the GetConfig(string sectionname) method's sectionname parameter.
Infact, the search just spilit the sectionname string into a string array
via the "/" flag , then recursively use XmlReader to search from the
confige file's xml document. It compare each found element's Qulified Name
with the one in the string Array. So if we specify namespace for our
configSection, it'll only compare the namespace prefix rather than the
namespaceURI. For example, if we have the following configSection:

<xs:root xmlns:xs="http://www.test.org">
.......
</xs:root>

We can locate the section via ConfigurationSettings.GetConfig("xs:root")

but the ConfigurationSettings.GetConfig("http://www.test.org:root") won't
work. Currently I think we could only do our custom xml parsing through the
means you mentiond:
1. Use general hierarchical sections

2. Put all the sub sections in a single root section(parsed by our custom
section handler). Then, we can differ the sub sections via namespaceURI

Thanks & Regards,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Thanks Steven,

If you have a place for feature requests, it would be very nice if the
Configuration class was namespace-aware, so that there could be two sections
like:

<!-- App 1 -->
<myStuff xmlns="urn:schemas-rdcpro-com:App1">
<foo>bar</foo>
</myStuff>

<!-- App 2 -->
<myStuff xmlns="urn:schemas-rdcpro-com:App2">
<foo>snafu</foo>
</myStuff>

so that two similar applications, especially those built from a "framework"
that had similar configuration constructs could co-exist in a web.config. An
excellent example is a forms processing service based on InfoPath forms. I
might have a fairly large number of forms processed by the same endpoint, but
each form belongs to a different Business system, and therefore might have
different configuration requirements even though the configuration *schema*
is the same!

I would suggest this feature be implemented by an overload for GetConfig()
that would accept both a section name as well as a namespace, as in:


public static object GetConfig(
string sectionName
);

public static object GetConfig(
string qualifiedName,
string namespaceURI
);



Regards,
Mike Sharp
 
S

Steven Cheng[MSFT]

Hi Mike,

Thanks for your followup. Yes, your suggestion about providing a further
override GetConfig method for retrieving namespaceURI based config section
really sounds great. Not sure whether our CLR guys will improve the
generally xml config file's manipulation in the next version. As we could
see , the WHIDBEY asp.net's web.config manipulation has been greatly
enhanced by adopting the feedback from many parnters and community members.
I think you can also submit your good request to our product feedback site:

http://lab.msdn.microsoft.com/productfeedback/default.aspx

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top