【发布时间】:2019-03-05 14:57:09
【问题描述】:
这是我的 WPF 应用程序中的 app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="XXX.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="XXX.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<userSettings>...</userSettings>
<applicationSettings>...</applicationSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
运行应用程序得到异常:
ConfigurationErrorsException: 只有一个元素 每个配置文件都允许,如果存在必须是 根元素
如果我删除 <runtime> 部分应用程序运行良好但会导致 bulid time 警告:
解决方案:https://stackoverflow.com/a/46533630/3907561
所以我的问题是如何使“配置”中的“configSections”“运行时”协同工作?
【问题讨论】:
-
这令人困惑。该错误的意思是“必须只有一个 configSections 部分,并且它必须是配置元素的第一个子元素”,但在您的配置中似乎就是这种情况。
<userSettings>或<applicationSettings>中是否还有另一个configSections声明? -
嗯,不知道是不是因为你声明了多个sectionGroups?
-
如果我删除
部分,除了构建时间警告外,一切都很好。 @stuartd
标签: c# nuget app-config