我正在尝试通过从 那里定向来使用配置文件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>
Run Code Online (Sandbox Code Playgroud)
看起来Environment.config如下:
<appSettings>
<add key="URL" value="http://foo.aspx"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
结果消息:
OneTimeSetUp: System.Configuration.ConfigurationErrorsException : 配置系统初始化失败 ----> System.Configuration.ConfigurationErrorsException : configSource 属性必须是相对物理路径,因此不允许使用“/”字符。(D:\tfs\QA - Automation\Projects\ReportAppeal\ReportAppeal\bin\Debug\ReportAppeal.dll.config 第 22 行)
我尝试从“/”切换到“\”,但出现了不同的错误。
结果消息:
System.Configuration.ConfigurationErrorsException:无法打开配置源文件“Config\Environment.config”。(D:\tfs\QA - Automation\Projects\ReportAppeal\ReportAppeal\bin\Debug\ReportAppeal.dll.config 第 22 行) TearDown:System.NullReferenceException:未将对象引用设置为对象的实例。
我可能需要改变引导文件的方式,Environment.config但我不确定如何改变。
正如错误所说:
configSource 属性必须是相对物理路径
因此,您需要将密钥更改为物理路径,而不是相对路径:
<appSettings configSource="C:\Config\Environment.config"/>
Run Code Online (Sandbox Code Playgroud)
或者将其保留在根目录下:
<appSettings configSource="Environment.config"/>
Run Code Online (Sandbox Code Playgroud)