asp.net将appSettings放在App.config中

时间:2015-10-20 11:11:19

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

我正在构建一个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>

到此(将<AppSettings>移到底部)

  <?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>

的工作。我更进了一步,继续向<AppSettings>移动,发现我可以放置的最高点位于<statup>正下方

我一直认为App.config中的每个元素都只是一个被调用的引用,而且它是必需的。

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

1 个答案:

答案 0 :(得分:0)

AFAIK您可以在configuration节点下的任何位置放置appsetting 我可以考虑元素放置的唯一限制是configSections元素。引自https://msdn.microsoft.com/en-us/library/aa903350.aspx

  

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

因此,您不能将appsettings(或任何元素)置于configSections之上。