首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将'System.Configuration.DefaultSection‘类型的对象强制转换为'System.Configuration.AppSettingsSection’类型

无法将'System.Configuration.DefaultSection‘类型的对象强制转换为'System.Configuration.AppSettingsSection’类型
EN

Stack Overflow用户
提问于 2019-08-02 01:11:41
回答 1查看 2.2K关注 0票数 1

我不明白为什么我会得到这个错误。我正在尝试从映射配置文件的appSettings部分获取一个值。

下面是我的配置文件:

代码语言:javascript
复制
<?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://mms.com/aaaa/"/>
    <add key="ServiceUrl" value="https://temp.com/aaaa/Inspection/Service"/>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
    <add key="EmailAddress" value="support@mms.com" />
  </appSettings>
  <connectionStrings>
    <add name="CustomerDB" connectionString="Provider=SQLOLEDB.1;Data Source=.\temp;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>

下面是我的代码:

代码语言:javascript
复制
                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的部分,但是当我这样做时,它是空的:

代码语言:javascript
复制
conf.GetSection("appSettings");

任何帮助都将不胜感激。

EN

Stack Overflow用户

回答已采纳

发布于 2019-08-02 02:11:56

我找到了解决方案:

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

代码语言:javascript
复制
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = path;
            Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

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

只需删除第一行上ExeConfigurationFileMap实例的参数即可解决此问题。如果这对任何人有帮助,我想我会把我的答案贴出来。

票数 6
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57313950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档