配置源路径使用错误

Configsource path using wrongly

我正在尝试通过从 App.config 定向到那里来使用配置文件。 我在我的解决方案中创建了一个名为 Config 的文件夹,并创建了一个名为 Environment.config.

的新配置文件

我的 App.Config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <appSettings configSource="Config/Environment.config"/>
</configuration>

并且 Environment.config 如下所示:

  <appSettings>
    <add key="URL" value="http://foo.aspx"/>
  </appSettings>   

我收到一条错误消息:

Result Message:
OneTimeSetUp: System.Configuration.ConfigurationErrorsException : Configuration system failed to initialize ----> System.Configuration.ConfigurationErrorsException : The configSource attribute must be a relative physical path, so the '/' character is not allowed. (D:\tfs\QA - Automation\Projects\ReportAppeal\ReportAppeal\bin\Debug\ReportAppeal.dll.config line 22)

我试图从“/”切换到“\”,但出现了不同的错误。

Result Message:
System.Configuration.ConfigurationErrorsException : Unable to open configSource file 'Config\Environment.config'. (D:\tfs\QA - Automation\Projects\ReportAppeal\ReportAppeal\bin\Debug\ReportAppeal.dll.config line 22) TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

我可能需要更改我引导 Environment.config 文件的方式,但我不确定如何。

如错误所述:

The configSource attribute must be a relative physical path

因此您需要将密钥更改为物理路径,而不是相对路径:

<appSettings configSource="C:\Config\Environment.config"/>

或者将其保留在根目录下:

<appSettings configSource="Environment.config"/>