c#中的自定义app.config生成配置系统初始化失败异常

custom app.config in c# generates configuration system failed to initialize exception

我检查了互联网上所有与配置系统初始化异常失败相关的解决方案,但其中 none 对我有用。 所以我发布了这个问题。

我正在使用 winform c# 开发 PayPal REST API。我自定义了 app.config 文件。当我重建应用程序时它显示 Configuration system failed to initialize

这里是默认app.config代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

这是我的自定义app.config代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
  </configSections>

<!--PayPal SDK settings-->
  <paypal>
    <settings>
      <add name="mode" value="sandbox" />
      <add name="clientId" value="__CLIENT_ID__" />
      <add name="clientSecret" value="__CLIENT_SECRET__" />
    </settings>
  </paypal>

</configuration>

编辑:

我用try catch块来捕获异常,否则程序不会终止但工作异常。我的调用堆栈是空的。我在表单加载时使用了 try catch。

通过将 <configSection> 作为 <configuration> 的第一个子元素解决了我的问题。

这是代码的重新排列。

这是新代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
 </configSections>
 <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>

 <!--PayPal SDK settings-->
 <paypal>
   <settings>
     <add name="mode" value="sandbox" />
     <add name="clientId" value="__CLIENT_ID__" />
     <add name="clientSecret" value="__CLIENT_SECRET__" />
   </settings>
 </paypal>

</configuration>