Problem: IConfigurationSectionHandler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

H

I've created my own custom configuration section handler class to interpret custom sections in my app config file. However I keep on getting a ConfigurationException when trying to access the section from code as in

Loftware.Diagnostics.CustomListeners listeners = (Loftware.Diagnostics.CustomListeners)ConfigurationSettings.GetConfig("customListeners")

The definition of the section handler in the app config file is
<configuration><configSections><section name="customListeners" type="Loftware.Diagnostics.CustomListenersHandler,Loftware.Diagnostics" /></configSections

The definition of the custom section within the app config file is
<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners

The problem seems to be with the format of the <customListeners> node in the app config file. If I change the <customListeners> to be a single line statement without any line feeds or tabs in the app config file as in following everything works fine

<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners

Why do I need to remove the line feeds and tabs to get it working? The complete exception dump is

System.Configuration.ConfigurationException: Exception in configuration section handler (C:\Development\VS .NET\C#\Loftware\Diagnostics\Test\bin\Debug\Loftware.Diagnostics.Test.exe.config line 102) ---> System.NullReferenceException: Object reference not set to an instance of an object
at Loftware.Diagnostics.CustomListeners..ctor(XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 15
at Loftware.Diagnostics.CustomListenersHandler.Create(Object parent, Object configContext, XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 1
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
--- End of inner exception stack trace --
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
at System.Configuration.ConfigurationRecord.Evaluate(String configKey
at System.Configuration.ConfigurationRecord.ResolveConfig(String configKey
at System.Configuration.ConfigurationRecord.GetConfig(String configKey
at System.Configuration.DefaultConfigurationSystem.System.Configuration.IConfigurationSystem.GetConfig(String configKey
at System.Configuration.ConfigurationSettings.GetConfig(String sectionName
at Loftware.Diagnostics.Test.Test..cctor() in c:\development\vs .net\c#\loftware\diagnostics\test\test.cs:line 3

Thank
Carel
 
Hell Carel,

Thanks for your post. As I understand, the problem you are facing is that
it throws ConfigurationException when it creates your CustomListeners from
the configuration section. Please correct me if there is any
misunderstanding.

Generally speaking, a configuration section need not to be in a single
line. That is, it can contain line feeds, etc. I think more information is
needed before moving forward:

What's the relationship between FileTraceListener and CustomListeners?
Is it possible to post a simple solution which is able to reproduce the
problem? I will be glad to check it on my side.

I look forward to hearing from you.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Tian Min

The solution is a bit too complex to paste in here. I have a test project - can I zip it up and send it somewhere where you can get hold of it?

Regards
Carel
 
Hello Carel,

You can sent it to my email address (e-mail address removed) directly, and
tell me the detailed steps to reproduce the problem.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
The problem seems to be with the format of the said:
If I change the <customListeners> to be a single line statement without any line feeds or
tabs in the app config file as in following everything works fine:

Yes, you're running into the problem that the XML stuff gets
interpreted into XML nodes, which also includes some "whitespace"
nodes!

Try this in your method:

public object Create(object parent, object configContext, XmlNode
section)
{
.......
foreach(XmlNode oNode in section.ChildNodes)
{
if(oNode.NodeType == XmlNodeType.Element)
{
// only in this case, use it!
}
}
.......
}

Of course, you could also test for additional node types, e.g.
XmlNodeType.Attribute etc., or maybe just excluding the
XmlNodeType.WhiteSpace will do.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top