0
votes

I am just trying to build my application without visual studio by using MSBuild.

Using

  • Visual Studio Build Tools 2017.
  • Dot Net framework 4.6.1 SDK
  • MSBuild auto-detection: using msbuild version '15.9.21.664'

Full Error Message :

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'.

Any idea why? Thanks...

Update

This is the dll path with visual studio

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

This is without visual studio, only with MSBuild

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

File path is different. How can I fix this?

2
Hi Loran, any update about this issue?Mr Qian
@PerryQian-MSFT, Your answer makes me some point and I found version different and reference location is different. So I just make it to reference to the same location and build again. now It works. But I still need to check. Thank you for your detailed answer.Loran
Besides, you can strongly set the hintpath of the System.Net.Http.dll to a specific path. And use that to make sure that they use the same path or similar path. Also, thanks for sharing your solution and glad to know the issue went away. I have added your solution into my answer and some details. You can check it. See my update 1 :) And if it helps, you could consider accept it.Mr Qian

2 Answers

1
votes

It seems that your project has used a newer version dll about System.Net.Http and if some dependencies use the old version 4.0.0.0 System.Net.Http.dll, it will miss the old one.

Besides, please the following steps:

1) make sure that your xxx.csproj file has this node, if not, you should modify like this:

<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>

Also, try to set Copy Local of System.Net.Http.dll to true.

Modify such node like this in xxx.csproj file:

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

2) open CMD as Administrator and then type:

     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 Studio\2017\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

3) Maybe you could try this in app.config file:

<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> 

There is a similar issue about it.

4) then delete bin and obj folder and then use Build Tool for VS2017 to build the project again.

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

Update 1

Thanks for sharing your solution. Actually, Not sure that whether system cannot find the System.Net.Http when you move your project into Build agent.

Actually, you can use hintpath to force the path to reference System.Net.Http in your project and make sure that they are the same. Also, this solution will make sure the dll will copied into output folder. And it will make VS or msbuild find and identify the DLL.

And you have changed the location of the DLLL to the same so that the issue is fixed.

You can try to use this in your xxx.csproj file:

It will also copy such dll into output folder.

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

or

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

In your web.config or App.config add:

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