无法将类型为“ System.Configuration.DefaultSection”的对象转换为类型为“ System.Configuration.AppSettingsSection”

时间:2019-08-01 17:11:41

标签: c# config

我无法弄清楚为什么出现此错误。我正在尝试从映射的配置文件的appSettings部分获取值。

这是我的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  </configSections>
  <appSettings>    
    <!-- Need to configure.-->
    <add key="ServiceDisplayName" value="Scheduler-aaaa" />
    <add key="ServiceName" value="SchedulerService-aaaa" />
    <add key="SchedulerTime" value="05:00 AM" />
    <!-- 12 hour format-->
    <add key="RoutingInterval" value="5"/>
    <add key="ADSyncInterval" value="2"/>
    <add key="ReportPath" value="C:\inetpub\wwwroot\aaaa\Reports\DefaultReports"/>
    <add key="ReportPathCustom" value="C:\inetpub\wwwroot\aaaa\Reports\CustomReports"/>
    <add key="ApplicationUrl" value="https://proteusmmx.com/aaaa/"/>
    <add key="ServiceUrl" value="https://proteusmmx.com/aaaa/Inspection/Service"/>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
    <add key="EmailAddress" value="support@eaglecmms.com" />
  </appSettings>
  <connectionStrings>
    <add name="ProteusMMXCustomerDB" connectionString="Provider=SQLOLEDB.1;Data Source=.\sql2016krisf2;User ID=mm;PWD=mm;Initial Catalog=35testupdate" />
  </connectionStrings>
  <cachingConfiguration />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

这是我的代码:

                ExeConfigurationFileMap fm = new ExeConfigurationFileMap(path);
                fm.ExeConfigFilename = path;//"Scheduler.exe.config";
                Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(fm, ConfigurationUserLevel.None);

                if (conf.AppSettings.Settings["SchedulerTime"] != null) //<<ERROR
                {
                    return conf.AppSettings.Settings["SchedulerTime"].Value;
                }

我尝试仅获取appSettings部分,但是当我这样做时它为null:

conf.GetSection("appSettings");

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

将我的代码更改为以下内容:

X foo<X>(X x) => x;

main() {
  int Function(int) f = (int x) => foo<int>(x);
  print(f(42));
  // But `f('Hello!')` is a type error.
}

只需在第一行中删除ExeConfigurationFileMap实例的参数即可解决此问题。我以为如果有帮助,我会发表我的答案。