Custom configuration sections --- why might GetSection return null?

M

mcstrini

I'm trying to have my application use a special config file which is
shared across servers, and want to include a custom section (for URLs
of report services associated with database instances). However, my
section doesn't seem to be recognized (GetSection("ReportServerUrls")
returns null). I don't really see how what I'm doing differs from the
usage examples I've found.

I have my XML config itself:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
[...]
<section name="ReportServerUrls"
type="[...]DbConfiguration.ReportServerUrlSection,
[...]DbConfiguration"/>
[...]
</configSections>
[...]
<ReportServerUrls>
<urls>
<add dbInstance="TEST" url=[...] />
</urls>
</ReportServerUrls>
</configuration>

the classes for the custom section, with appropriate attributes:
public class ReportServerUrlElement : ConfigurationElement
{
private static ConfigurationProperty propDbInstance;
private static ConfigurationProperty propUrl;

[ConfigurationProperty("dbInstance", IsRequired = true)]
public string DbInstance
{
get { return (string)base[propDbInstance]; }
}

[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)base[propUrl]; }
}

protected override ConfigurationPropertyCollection Properties
{
get
{
return base.Properties;
}
}

static ReportServerUrlElement()
{
propDbInstance =
new ConfigurationProperty(
"dbInstance", typeof(string), null,
ConfigurationPropertyOptions.IsRequired
);
propUrl =
new ConfigurationProperty(
"url", typeof(string), null,
ConfigurationPropertyOptions.IsRequired
);
}
}

[ConfigurationCollection(typeof(ReportServerUrlElement),
CollectionType =
ConfigurationElementCollectionType.AddRemoveClearMap)]
public class ReportServerUrlCollection :
ConfigurationElementCollection
{
public override ConfigurationElementCollectionType
CollectionType
{
get { return base.CollectionType; }
}
public ReportServerUrlElement this[int index]
{
get { return
(ReportServerUrlElement)base.BaseGet(index); }
}
public ReportServerUrlElement this[string dbInstance]
{
get { return
(ReportServerUrlElement)base.BaseGet(dbInstance); }
}

private static ConfigurationPropertyCollection properties;

protected override ConfigurationPropertyCollection Properties
{
get { return properties; }
}

static ReportServerUrlCollection()
{
properties = new ConfigurationPropertyCollection();
}

protected override ConfigurationElement CreateNewElement()
{
return new ReportServerUrlElement();
}

protected override object GetElementKey(ConfigurationElement
element)
{
return (element as ReportServerUrlElement).DbInstance;
}
}

public class ReportServerUrlSection : ConfigurationSection
{
private static ConfigurationProperty propUrls;
private static ConfigurationPropertyCollection properties;

[ConfigurationProperty("urls")]
public ReportServerUrlCollection Urls
{
get { return (ReportServerUrlCollection)
(base[propUrls]); }
}

public ReportServerUrlSection() : base()
{
propUrls =
new ConfigurationProperty(
"urls",
typeof(ReportServerUrlCollection),
new ReportServerUrlCollection(),
ConfigurationPropertyOptions.None
);

properties = new ConfigurationPropertyCollection();
properties.Add(propUrls);
}
}

and then, as far as I can tell, this is going into a DLL with the same
name included in the "type" attribute of the "section" element in the
config XML, in the same directory with the executables and the config
file itself. (I'm opening the config file with
OpenExeConfiguration(path)). But config.GetSection("ReportServerUrls")
still returns null. Any ideas on what could be the problem (or even
what I could do to get more info. on the problem)?
 
M

mcstrini

I'm trying to have my application use a special config file which is
shared across servers

Sorry, I meant "shared across multiple executables" (specifically, a
client app and a server-configuration app).
 

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

Top