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

net472项目调试出现System.Net.Http程序集加载异常咨询解决方案

问题背景

正在开发大型应用,依赖多个内部包实现数据获取功能。项目可正常完成构建,但调试新增代码段时触发报错:首先在mscorlib.dll中抛出System.IO.FileNotFoundException异常,随后出现未被用户代码处理的System.AggregateException异常,提示「发生一个或多个错误」。

调试窗口原始输出

'testhost.net472.x86.exe' (CLR v4.0.30319: Domain 3): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'testhost.net472.x86.exe' (CLR v4.0.30319: TestSourceHost: Enumerating source (C:\MyProJ\ProjectTests\bin\Debug\.netframework,version=v4.7.2\ProjectTests.dll)): Loaded 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\TestPlatform\.... .dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
....
....

Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll
An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code
One or more errors occurred.

...
...
'testhost.net472.x86.exe' (CLR v4.0.30319: TestSourceHost: Enumerating source (C:\MyProJ\ProjectTests\bin\Debug\.netframework,version=v4.7.2\ProjectTests.dll)): Loaded 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Remote Debugger\x86\Runtime\Microsoft.VisualStudio.Debugger.Runtime.Desktop.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'testhost.net472.x86.exe' (CLR v4.0.30319: Domain 3): Unloaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'

测试资源管理器报错信息

System.AggregateException: 发生一个或多个错误。 ---> System.IO.FileNotFoundException: 未能加载文件或程序集“System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。 ---> System.IO.FileNotFoundException: 未能加载文件或程序集“System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。

项目框架配置

  • 主项目目标框架:
<TargetFramework>net472</TargetFramework>
  • 依赖的内部库目标框架:
<TargetFrameworks>netstandard2.0; net472</TargetFrameworks>
问题根因

这是.NET Framework搭配netstandard2.0依赖时非常典型的程序集版本冲突,核心问题出在System.Net.Http的版本匹配逻辑上:

  1. 主项目基于net472运行,系统全局程序集缓存(GAC)里自带的System.Net.Http默认是4.0.0.0版本
  2. 依赖的内部库为多目标编译,同时支持netstandard2.0和net472,其中netstandard2.0版本依赖的System.Net.Http版本为4.2.0.0
  3. 构建阶段仅校验引用是否存在,不会验证运行时所有子依赖的加载路径,因此构建可以正常通过;但运行测试时testhost进程查找对应版本dll失败,先尝试加载4.2.0.0版本失败,回退加载4.0.0.0版本也失败,最终异常被包装为AggregateException抛出
  4. 该问题仅在测试场景触发的常见原因是,测试宿主的程序集加载逻辑和主程序启动逻辑有差异,不会自动继承主项目的绑定重定向规则
可行解决方案

按优先级从高到低依次尝试:

  • 方案1:开启自动绑定重定向
    给主项目和测试项目都开启自动生成绑定重定向配置,直接在对应项目的csproj文件中添加如下配置段:
    <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
    
    重新生成项目后,检查输出目录下的*.dll.config/*.exe.config文件,确认已生成System.Net.Http的绑定重定向规则,将全版本范围的加载请求统一重定向到输出目录中存在的版本。
  • 方案2:手动配置绑定重定向
    如果自动重定向未生效,直接在项目的App.config中手动添加程序集绑定规则,强制运行时加载可用版本:
    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    
    注意:如果本地输出目录没有4.2.0.0版本的System.Net.Http,可以通过NuGet安装对应版本的包,或者将newVersion改为本地GAC中存在的4.0.0.0版本验证。
  • 方案3:统一依赖引用规则
    给主项目、测试项目显式安装和内部库依赖版本完全一致的System.Net.Http NuGet包,避免不同项目引用不同版本的程序集;如果可以调整内部库引用逻辑,优先让主项目引用内部库的net472目标版本,而非netstandard2.0版本,减少跨框架兼容带来的版本冲突。
  • 方案4:补充测试项目复制配置
    针对testhost找不到输出目录dll的问题,在测试项目的csproj中添加如下配置,强制将所有依赖项复制到输出目录:
    <PropertyGroup>
      <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    </PropertyGroup>
    

内容的提问来源于stack exchange,提问作者Saif Haider

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 19:45:56