使用错误的配置路径

问题描述 投票:4回答:1

我试图通过从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>   

我收到的错误是:

结果消息: OneTimeSetUp:System.Configuration.ConfigurationErrorsException:配置系统初始化失败----> System.Configuration.ConfigurationErrorsException:configSource属性必须是相对物理路径,因此不允许使用'/'字符。 (D:\ tfs \ QA - Automation \ Projects \ ReportAppeal \ ReportAppeal \ bin \ Debug \ ReportAppeal.dll.config第22行)

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

结果消息: System.Configuration.ConfigurationErrorsException:无法打开configSource文件'Config \ Environment.config'。 (D:\ tfs \ QA - Automation \ Projects \ ReportAppeal \ ReportAppeal \ bin \ Debug \ ReportAppeal.dll.config第22行)TearDown:System.NullReferenceException:未将对象引用设置为对象的实例。

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

c# app-config
1个回答
4
投票

正如错误所说:

configSource属性必须是相对物理路径

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

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

或者只是将它留在根目录下:

<appSettings configSource="Environment.config"/>
© www.soinside.com 2019 - 2024. All rights reserved.