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

如何强制Visual Studio使用NuGet中的System.Net.Http而非引用程序集?

解决方案:强制使用NuGet包中的System.Net.Http.dll

一、让Visual Studio属性面板显示并使用NuGet引用版本

  1. 修改项目的.csproj文件,给System.Net.Http的引用添加<SpecificVersion>True</SpecificVersion>,强制VS严格按照指定版本和路径加载:
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\lib\Nugets\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
  <Private>True</Private>
  <SpecificVersion>True</SpecificVersion>
</Reference>
  1. 执行以下操作确保引用生效:
    • 关闭Visual Studio
    • 删除项目目录下的obj和bin文件夹
    • 重新打开项目,右键项目 → 管理NuGet程序包 → 找到System.Net.Http,点击「重新安装」

二、强制bin/roslyn目录使用NuGet包的DLL

roslyn目录中的文件由编译服务依赖引入,可通过添加自定义复制任务强制替换:

  1. 在.csproj文件末尾添加以下Target节点(注意替换为你实际的NuGet包路径):
<Target Name="CopyNuGetSystemNetHttpToRoslyn" AfterTargets="Build">
  <Copy SourceFiles="$(SolutionDir)lib\Nugets\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll"
        DestinationFolder="$(OutputPath)roslyn"
        OverwriteReadOnlyFiles="True" />
</Target>
  1. 同时在.csproj的<PropertyGroup>中添加属性,禁用系统版本的自动引用:
<PropertyGroup>
  <DisableImplicitSystemNetHttpReference>True</DisableImplicitSystemNetHttpReference>
</PropertyGroup>

三、配置运行时绑定重定向确保版本一致

在项目的web.config中添加绑定重定向,保证运行时加载正确版本:

<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.1.1.3" newVersion="4.1.1.3" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 09:45:49