类型 'HttpResponseMessage' 在未引用的程序集中定义

The type 'HttpResponseMessage' is defined in an assembly that is not referenced

我只是想在没有 visual studio 的情况下使用 MSBuild 构建我的应用程序。

正在使用

完整的错误信息:

error CS0012: The type 'HttpResponseMessage' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

知道为什么吗?谢谢...

更新

这是visual studio

的dll路径

C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

这没有 visual studio,只有 MSBuild

C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\CredentialProvider.Microsoft

文件路径不同。我该如何解决这个问题?

在您的 web.config 或 App.config 中添加:

          <compilation debug="true" targetFramework="4.6.1" />
             <assemblies>
                <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
             </assemblies>
          </compilation>
   

看来你的项目使用了一个新版本的dll System.Net.Http,如果一些依赖使用旧版本4.0.0.0 System.Net.Http.dll,它会错过旧版本。

外,请进行以下步骤:

1)确保你的xxx.csproj文件有这个节点,如果没有,你应该这样修改:

<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>

,尝试将System.Net.Http.dll复制本地设置为true

xxx.csproj文件中像这样修改这样的节点:

<Reference Include="System.Net.Http">
     <Private>True</Private>
  </Reference>   

2)管理员身份打开 CMD 然后输入:

     cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
               
     gacutil /i xxxxx\System.Net.Http.dll(path where the System.Net.Http.dll exists)

 // like the path C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

3) 也许你可以在 app.config 文件中试试这个:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
         <dependentAssembly>  
            <assemblyIdentity name="System.Net.Http"  
                              publicKeyToken="32ab4ba45e0a69a1"  
                              culture="neutral" />  
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.0"  
                             newVersion="4.2.0.0"/>  
         </dependentAssembly>  
      </assemblyBinding>  
   </runtime>  
</configuration> 

.

4)然后删除binobj文件夹然后使用VS2017构建工具构建再次项目。

============================================= ========

更新 1

感谢您分享您的解决方案。实际上,当您将项目移动到 Build agent 时,不确定系统是否找不到 System.Net.Http

实际上,您可以使用hintpath 强制在您的项目中引用System.Net.Http 的路径,并确保它们是相同的。此外,此解决方案将确保将 dll 复制到输出文件夹中。并且它会让VS或msbuild找到并识别DLL。

并且您已将 DLLL 的位置更改为相同的位置,以便问题得到解决。

您可以尝试在您的 xxx.csproj 文件中使用它:

它还会将此类 dll 复制到输出文件夹中。

<Reference Include="System.Net.Http">
      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll</HintPath>
    </Reference>

<Reference Include="System.Net.Http">
          <HintPath>C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll</HintPath>
        </Reference>