locked
FileConfigurationSource Not Loading RRS feed

  • Question

  • User188029144 posted

    I cannot seem to get my externally configured connectionstrings section to be loaded on the app startup.  Any advice?

    Code:

    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["default"];
    if (settings == null || settings.ConnectionString.Trim() == "")
      throw new ConfigurationErrorsException("No default connection string.");
    

    Error:

    System.Configuration.ConfigurationErrorsException: No default connection string.

    web.config:

    <configuration>
        <configSections>
            <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </configSections>
        <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
            <sources>
                <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                  filePath="demo.config" />
            </sources>
        </enterpriseLibrary.ConfigurationSource>
    </configuration>

    and demo.config:

    <?xml version="1.0"?>
    <configuration>
        <connectionStrings>
            <clear/>
            <add name="default" connectionString="User Id=etc., etc."/>
        </connectionStrings>
    </configuration>


    Tuesday, July 21, 2009 11:39 AM

Answers

  • User188029144 posted
    <abbr></abbr>

    <input name="ctl00$ctl00$MasterContent$Content$PostRepeater$ctl18$PostId" id="ctl00_ctl00_MasterContent_Content_PostRepeater_ctl18_PostId" value="214970" type="hidden">

    Configuration settings in FileConfigSource are are not accessible via ConfigurationManager.

    The best solution here is to use configSource attribute of the individual config sections as Tom Hollander points out in his blog:

    http://blogs.msdn.com/tomholl/archive/2006/04/02/entlib2externalconfig.aspx

    In this case:

    <connectionStrings configSource="demo.config" />

    where demo.config includes only the configuration element of interest:

    <connectionStrings>
    <add name="Tom's Connection" connectionString="Database=EntLibQuickStarts;Server=(local)\SQLEXPRESS;Integrated Security=SSPI;"
    providerName="System.Data.SqlClient" />
    </connectionStrings>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 22, 2009 5:17 PM