请求协助解决System.IdentityModel程序集加载失败异常
我来帮你排查这个System.IdentityModel加载失败的问题,这个BadImageFormatException通常和程序集损坏、版本不兼容或者平台架构不匹配有关,试试下面这些针对性的解决步骤:
可能的解决方案
检查程序集完整性
前往.NET Framework安装目录(32位系统:C:\Windows\Microsoft.NET\Framework\v4.0.30319;64位系统:C:\Windows\Microsoft.NET\Framework64\v4.0.30319)找到System.IdentityModel.dll:- 右键文件选择「属性」,查看「详细信息」标签页的版本是否为
4.0.0.0 - 使用
ildasm.exe(Visual Studio命令工具)打开该文件,确认是否存在程序集清单(如果没有则说明程序集已损坏)
- 右键文件选择「属性」,查看「详细信息」标签页的版本是否为
修复.NET Framework 4.x
打开「控制面板」→「程序和功能」,找到Microsoft .NET Framework 4.x系列版本,点击「更改」→选择「修复」,完成后重启服务器/本地电脑,让修复生效。验证项目引用配置
右键你的Web项目→「引用」,找到System.IdentityModel:- 确认引用的版本是
4.0.0.0 - 右键该引用→「属性」,将「复制本地」设置为
True - 如果引用缺失,重新添加:右键引用→「添加引用」→「程序集」→「框架」,找到
System.IdentityModel勾选添加
- 确认引用的版本是
调整平台目标设置
右键Web项目→「属性」→「生成」:- 将「平台目标」设置为
Any CPU(如果服务器是64位,不要勾选「首选32位」) - 确保项目的平台架构和服务器运行环境一致(比如服务器是64位,项目不要强制设为x86)
- 将「平台目标」设置为
清理重建项目
- 删除项目目录下的
bin和obj文件夹 - 在Visual Studio中点击「生成」→「清理解决方案」,然后再点击「生成解决方案」,避免旧的损坏程序集残留
- 删除项目目录下的
启用程序集绑定日志排查(可选)
按照错误提示的方法启用日志,能更精准定位问题:- 打开注册表编辑器(
regedit) - 定位到
HKLM\Software\Microsoft\Fusion - 新建
DWORD值EnableLog,设置值为1 - 重现错误后,日志会生成在
%windir%\assembly\log\BindLog目录 - 排查完成后记得将
EnableLog改回0,避免性能损耗
- 打开注册表编辑器(
你遇到的错误详情:
无法加载文件或程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'或其依赖项。模块应包含程序集清单。
描述:当前Web请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中起源的更多信息。
异常详细信息:System.BadImageFormatException: 无法加载文件或程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'或其依赖项。模块应包含程序集清单。
源错误:当前Web请求执行期间生成了未处理的异常。可使用下面的异常堆栈跟踪确定异常的来源和位置。
程序集加载跟踪:以下信息有助于确定无法加载程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'的原因。
警告:程序集绑定日志已关闭。要启用程序集绑定失败日志记录,请将注册表值[HKLM\Software\Microsoft\Fusion!EnableLog](DWORD)设置为1。注意:程序集绑定失败日志记录会带来一些性能损失。要关闭此功能,请删除注册表值[HKLM\Software\Microsoft\Fusion!EnableLog]。堆栈跟踪:
[BadImageFormatException: 无法加载文件或程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'或其依赖项。模块应包含程序集清单。]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +21
System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38[ConfigurationErrorsException: 无法加载文件或程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'或其依赖项。模块应包含程序集清单。]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +738
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +68
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +62
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +108
System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +656[HttpException (0x80004005): 无法加载文件或程序集'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'或其依赖项。模块应包含程序集清单。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +4550932
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +94
内容的提问来源于stack exchange,提问作者husseinbaalbaki




