无法将类型 'System.Configuration.DefaultSection' 的对象转换为类型 'System.Configuration.AppSettingsSection'
Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'
我不明白为什么会出现此错误。我正在尝试从映射配置文件的 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://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>
这是我的代码:
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 部分,但是当我这样做时它是空的:
conf.GetSection("appSettings");
如有任何帮助,我们将不胜感激。
我找到了解决方案:
将我的代码更改为以下内容:
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 实例的参数即可解决问题。如果这对任何人有帮助,我想我会 post 我的回答。
我不明白为什么会出现此错误。我正在尝试从映射配置文件的 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://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>
这是我的代码:
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 部分,但是当我这样做时它是空的:
conf.GetSection("appSettings");
如有任何帮助,我们将不胜感激。
我找到了解决方案:
将我的代码更改为以下内容:
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 实例的参数即可解决问题。如果这对任何人有帮助,我想我会 post 我的回答。