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

.NET Framework 4.7.2 WebForm应用System.Net.Http版本冲突求助

.NET Framework 4.7.2 Web Form应用System.Net.Http版本冲突解决方案

问题根源

bin目录里的4.7.3062.0版本是.NET Framework 4.7.2内置的System.Net.Http程序集。从.NET 4.6开始,这个组件被整合进框架,编译时Visual Studio会自动优先引用框架自带版本,哪怕你安装了对应NuGet包,也会被框架版本覆盖,导致绑定重定向失效。

解决步骤

1. 强制所有项目使用NuGet包版本

打开每个引用System.Net.Http的项目(包括主Web应用)的.csproj文件,添加以下配置:

<PropertyGroup>
  <!-- 自动生成绑定重定向并输出到配置文件 -->
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
  <Reference Include="System.Net.Http">
    <!-- 指向你本地NuGet包的实际路径,根据项目结构调整 -->
    <HintPath>$(SolutionDir)packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
    <!-- 确保DLL被复制到bin目录 -->
    <Private>True</Private>
    <!-- 强制使用指定版本,拒绝自动替换 -->
    <SpecificVersion>True</SpecificVersion>
  </Reference>
</ItemGroup>

如果项目使用PackageReference而非packages.config,HintPath可改为$(NuGetPackageRoot)system.net.http\4.3.4\lib\net46\System.Net.Http.dll。

2. 修正web.config绑定重定向

替换原有的重定向规则,添加精确匹配和代码基指定:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" 
                          publicKeyToken="b03f5f7f11d50a3a" 
                          culture="neutral" />
        <!-- 重定向所有旧版本到4.3.4.0 -->
        <bindingRedirect oldVersion="0.0.0.0-4.3.4.0" newVersion="4.3.4.0" />
        <!-- 指定运行时从bin目录加载该版本 -->
        <codeBase version="4.3.4.0" href="bin\System.Net.Http.dll" />
      </dependentAssembly>
    </assemblyBinding>
    <!-- 禁用框架程序集的自动绑定干扰 -->
    <disableRuntimeBindingRedirects>false</disableRuntimeBindingRedirects>
  </runtime>
</configuration>

3. 彻底清理重建

  • 右键解决方案 → 清理解决方案
  • 删除所有项目下的bin和obj文件夹
  • 右键解决方案 → 还原NuGet包
  • 重新编译整个解决方案

4. 排查多目标库的隐性引用

即便多目标库未显式引用System.Net.Http,也要检查它们的输出目录是否包含该DLL。如果有,手动删除这些DLL,确保所有组件统一使用NuGet包版本。

5. 关闭VS自动引用框架程序集

进入Visual Studio的工具 → 选项 → 项目和解决方案 → 构建并运行,取消勾选自动引用框架程序集(若存在该选项),防止编译时自动添加框架自带的System.Net.Http引用。

验证

编译完成后,查看bin目录中的System.Net.Http.dll属性:

  • 详细信息里的文件版本应为4.6.26907.01(对应NuGet包4.3.4的实际文件版本)
  • 运行应用,确认FileLoadException不再出现

内容的提问来源于stack exchange,提问作者Robert Achmann

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 04:53:15