Question

So I have a custom config file as follows:

(myConfig.cfg)

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="wcfSettings" type="Maxeta.Services.Configuration.maxServiceSection"/>
  </configSections>
  <wcfSettings>
    <connectionStrings>
      <add name="SQLL3" connectionString="Data Source=|DataDirectory|Test.db; Version=3; New=False; Compress=False;" providerName="System.Data.SQLite" databaseDialect="Standard"/>
      <add name="SQLCE" connectionString="Data Source=|DataDirectory|Test.sdf; Max Database Size=256; Persist Security Info=False;" providerName="System.Data.SqlServerCe.4.0" databaseDialect="Standard" />
      <add name="MSSQL" connectionString="Data Source=127.0.0.1; Initial Catalog=Test; Persist Security Info=true; User ID=****; PWD=****;" providerName="System.Data.SqlClient" databaseDialect="MsSql2005" />
    </connectionStrings>
  </wcfSettings>
</configuration>

And I'm accessing it as follows:

(Default.aspx.cs)

protected void Page_Load(object sender, EventArgs e) {
  String cfg = String.Format("{0}myConfig.cfg", HttpRuntime.AppDomainAppPath);
  Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = cfg }, ConfigurationUserLevel.None);
  maxServiceSection section = config.GetSection("wcfSettings") as maxServiceSection;
  foreach (maxServiceElement element in section.ConnectionStrings) {
    ltrText.Text += String.Format("{0}<br/>", element.DatabaseDialect);
  }
}

And the error I get back is:

An error occurred creating the configuration section handler for wcfSettings: Could not load type 'Maxeta.Services.Configuration.maxServiceSection' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (C:\Users\JHorvath\Documents\Visual Studio 2010\Examples\WebSite\C#\CustomConfig\CustomConfig\myConfig.cfg line 4)

However, if I don't load this from a separate file, and use the web.config with this code:

(Default.aspx.cs)

protected void Page_Load(object sender, EventArgs e) {
  maxServiceSection section = ConfigurationManager.GetSection("wcfSettings") as maxServiceSection;
  foreach (maxServiceElement element in section.ConnectionStrings) {
    ltrText.Text += String.Format("{0}<br/>", element.DatabaseDialect);
  }
}

Everything works... can someone explain how to use the built in ConfigurationManager.OpenMappedExeConfiguration functionality to load a custom config with a custom configuration section and read that data. Seems like this should be a difficult thing to do, but for the life of me, I can't figure it out.

Was it helpful?

Solution

Turns out, I needed to override my ConfigurationElementCollection properties for this[int index] and this[string key] in order to access the elements directly. Once I defined those I was able to get everything working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top