【问题标题】:asp.net placing the appSettings in App.configasp.net 将 appSettings 放在 App.config 中
【发布时间】:2016-01-19 00:30:38
【问题描述】:

我正在构建一个 Windows 服务应用程序并收到以下错误

在 mscorlib.dll 中发生“System.TypeInitializationException”类型的未处理异常

经过几次实验后,我发现我的 App.config 文件从此更改

  <?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <appSettings>
      <add key="BancTecFilePath" value="C:\\FTPFolders\\BancTec\\"/>
    </appSettings>
    <configSections>
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      <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>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="SamplesEntities" connectionString="metadata=res://*/Model.SamplesEntities.csdl|res://*/Model.SamplesEntities.ssdl|res://*/Model.SamplesEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=unit6cog10sql01;initial catalog=Samples;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <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>
  </configuration>

到这个(将&lt;AppSettings&gt;移到底部)

  <?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <configSections>
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      <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>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="SamplesEntities" connectionString="metadata=res://*/Model.SamplesEntities.csdl|res://*/Model.SamplesEntities.ssdl|res://*/Model.SamplesEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=unit6cog10sql01;initial catalog=Samples;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <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>
    <appSettings>
      <add key="BancTecFilePath" value="C:\\FTPFolders\\BancTec\\"/>
    </appSettings>
  </configuration>

工作。我更进一步,继续向上移动&lt;AppSettings&gt;,发现我可以放置的最高位置在&lt;statup&gt;的正下方

我一直认为App.config 中的每个元素只是一个引用,将被称为 as 并且它是必需的。

我的问题是后台发生了什么导致此错误以及为什么它位于它所在的位置

【问题讨论】:

    标签: asp.net windows-services app-config


    【解决方案1】:

    AFAIK 您可以将 appsettings 放置在 configuration 节点下的任何位置。
    我能想到的元素放置的唯一限制是configSections 元素。引用https://msdn.microsoft.com/en-us/library/aa903350.aspx

    如果此元素在配置文件中,则它必须是第一个子元素 元素的元素。

    所以你不能将appsettings(或任何元素)放在configSections之上。

    【讨论】:

      猜你喜欢
      • 2011-12-12
      • 2015-06-05
      • 2018-11-09
      • 2018-05-08
      • 2011-12-17
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多