求助:.NET Standard 2.0中ConfigurationManager.GetSection抛出继承异常
以下是针对该异常的具体排查和解决步骤:
检查接口引用的正确性
.NET Framework与System.Configuration.ConfigurationManagerNuGet包中的IConfigurationSectionHandler属于不同环境的实现,需确保Custom.Config.Configurator类引用的是NuGet包提供的接口。打开类文件确认using System.Configuration;指向的是安装的NuGet包程序集,而非.NET Framework自带的系统程序集。验证配置节类型定义
核对配置文件中type属性的完全限定名:Custom.Config.Configurator, Custom,确保类的命名空间、类名无拼写错误,程序集名称(逗号后的Custom)与项目输出的程序集名称完全一致(注意大小写、强命名程序集需补充版本/公钥等完整信息)。添加程序集绑定重定向
在项目的app.config或web.config中添加绑定重定向,强制加载NuGet包版本的System.Configuration.ConfigurationManager,避免版本冲突:<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Configuration.ConfigurationManager" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>确认接口实现的完整性
确保Configurator类正确实现IConfigurationSectionHandler的Create方法,方法签名必须严格匹配:public object Create(object parent, object configContext, XmlNode section) { // 自定义配置解析逻辑 }避免显式实现时的签名错误,或遗漏方法实现。
清理并重新生成项目
删除项目的bin和obj目录,清理旧的编译输出,然后重新生成整个解决方案,确保所有依赖程序集都是最新版本。
内容的提问来源于stack exchange,提问作者lunatic84

