You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试ASP.NET应用时遇System.ServiceModel加载失败的解决求助

问题描述

Windows 11 + VS2022环境,此前可正常调试ASP.NET应用,近期调试任意应用均报错:

无法加载文件或程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。模块应包含程序集清单。

已尝试更换.NET Framework版本、重装框架、通过NuGet安装primitives包等方法,均未解决问题。完整错误堆栈信息如下:

Server Error in '/' Application.
无法加载文件或程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。模块应包含程序集清单。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.BadImageFormatException: 无法加载文件或程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。模块应包含程序集清单。

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

程序集加载跟踪: 下列信息有助于确定程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”无法加载的原因。


警告: 程序集绑定日志记录已关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。
注意: 与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

堆栈跟踪:


[BadImageFormatException: 无法加载文件或程序集“System.ServiceModel, 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.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +232
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +113
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +23
   System.Reflection.Assembly.Load(String assemblyString) +35
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48

[ConfigurationErrorsException: 无法加载文件或程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。模块应包含程序集清单。]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +767
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +85
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +287
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +69
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +137
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +854

[HttpException (0x80004005): 无法加载文件或程序集“System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。模块应包含程序集清单。]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +532
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +719

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.8.9037.0

解决方法

  • 检查System.ServiceModel.dll文件完整性
    找到.NET Framework安装目录下的System.ServiceModel.dll(通常路径为C:\Windows\Microsoft.NET\Framework\v4.0.30319\或Framework64对应目录),对比正常机器上的文件大小和哈希值,若文件损坏,从相同版本的.NET Framework环境中复制替换,或使用sfc /scannow命令修复系统文件。

  • 启用程序集绑定日志排查具体依赖
    按错误提示修改注册表启用绑定日志:

    1. 打开注册表编辑器(regedit)
    2. 定位到HKLM\Software\Microsoft\Fusion
    3. 创建或修改EnableLog(DWORD类型),设置值为1
    4. 重新运行调试,查看生成的日志文件,定位具体无法加载的依赖项
  • 检查项目平台目标与系统架构匹配
    在VS2022中,右键项目→属性→生成,确认“平台目标”设置为Any CPU、x86或x64,与当前系统架构(Windows 11通常为x64)一致,避免因32/64位不兼容导致加载失败。

  • 清理VS缓存与项目临时文件

    1. 关闭VS2022,删除项目目录下的bin、obj文件夹
    2. 删除VS缓存目录:%LOCALAPPDATA%\Microsoft\VisualStudio\17.0_*\ComponentModelCache(17.0对应VS2022版本号)
    3. 重新打开项目并生成调试
  • 修复或重装Visual Studio 2022
    打开Visual Studio Installer,选择“修改”→“修复”,若修复无效,尝试卸载后重新安装VS2022,确保勾选ASP.NET和Web开发相关组件。

内容的提问来源于stack exchange,提问作者Nwachukwu A. Nnaemeka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:41:03