58

I have several projects in my VS solution. Whenever I add "System.Net.Http" NuGet package to one it shows as version 4.2.0.0. Then I do the same and add same NuGet Package, however, the other says version. 4.1.1.2

enter image description here enter image description here

Then I get a warning:

Found conflicts between System.Net.Http

EDIT1:

Gathering dependency information took 1.7 sec
Attempting to resolve dependencies for package 'System.Net.Http.4.3.3' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'System.Net.Http.4.3.3'
Resolved actions to install package 'System.Net.Http.4.3.3'
Retrieving package 'System.Net.Http 4.3.3' from 'nuget.org'.
Adding package 'System.Net.Http.4.3.3' to folder 'C:\...Service\packages'
Added package 'System.Net.Http.4.3.3' to folder 'C:\...Service\packages'
Added package 'System.Net.Http.4.3.3' to 'packages.config'
Successfully installed 'System.Net.Http 4.3.3' to ....Service
Executing nuget actions took 2.05 sec
Time Elapsed: 00:00:03.8937113

Please notice correct version installed, However => Props => Version says 4.1.1.2

enter image description here

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • Are the projects targeting the exact same framework version? – Camilo Terevinto Feb 19 '18 at 12:41
  • yes. all are 4.6.1 – ShaneKm Feb 19 '18 at 12:41
  • Is it likely you've got something which depends on 4.1.1.2 in the project that Nuget is pulling that version for? Worst case you can just install 4.1.1.2 in the other project using the `-Version` command line option – Charleh Feb 19 '18 at 12:45
  • 2
    I uninstalled ALL nuget packages, removed all refereces from other projects. Then Added System.Net.Http NuGet again. Still says 4.1.1.2. – ShaneKm Feb 19 '18 at 12:52
  • 2
    I've noticed that this happens when you create a new service fabric Stateful project. The target framework says 4.6.1. When you add "System.net.http"NuGet to that project it will reference .net.http ver 4.1.1.2. However, when you add a new Class Library, targeting the same .net frawork (4.6.1) and perform the same steps of adding the same NuGet package (System.Net.Http). It will reference ver 4.2.0.0. Not sure how to fix this – ShaneKm Feb 19 '18 at 13:01
  • See also [Strange issue with System.Net.Http 4.2.0.0 not found](//stackoverflow.com/q/47570912) – Michael Freidgeim Dec 20 '20 at 00:28
  • @ShaneKm, have you ever got to the bottom of this issue, especially in the context of SF? – abatishchev Dec 04 '21 at 00:55

11 Answers11

80

Edit: This happens only when using .NET Framework. In .NET Core/Standard land, the latest System.Net.Http assembly version seems to be always 4.1.2.0 - there is no 4.2.0.0 version available.

The issue regarding System.Net.Http is way, way more complicated then the answers here seem to imply...

  1. Yes, there is a System.Net.Http NuGet package, but no, it will not install the latest version of the same assembly (it contains version 4.1.1.2 of the System.Net.Http assembly, not 4.2.0.0).
    Update 19Mar2022: the latest NuGet package Version 4.3.4 has assembly version 4.1.1.3, assembly version 4.2.0.0 still is not directly installed.
  2. Latest Microsoft Visual Studio (or Microsoft Visual Studio Build Tools) provides version 4.2.0.0, but that does not mean your .csproj will always use it...
  3. For some reason (which I was not able to understand yet), the only guaranteed way of using 4.2.0.0 is by referencing certain NuGet packages that uses it, such us System.Buffers (version 4.5.0 worked for me).

TL;DR:

Add System.Buffers 4.5.0+ NuGet reference to your project, if you want to make sure it is using System.Net.Http 4.2.0.0 assembly.

References:

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
rsenna
  • 11,775
  • 1
  • 54
  • 60
  • 4
    This has solved my issue. I had a mix of .NET framework and aspnetcore projects running together. – Reuel Ribeiro Jul 30 '18 at 20:36
  • 10
    This resolved the issue for me as well. Holy jeebus Microsoft .net core team, this is a mess.I spent nearly 2 days trying to solve this on a large project that introduced .net standard libraries. – Michael Brown Aug 01 '18 at 05:45
  • I've added [an answer](https://stackoverflow.com/a/52102147/197591) that I based on information I found in this answer and the cited references that shouldn't require to add a random reference like `System.Buffers`. It's more long-winded, but hopefully it will help. – Neo Aug 31 '18 at 13:19
  • 1
    Really frustrating issue with a really unconventional workaround, but I appreciate you figuring this out and sharing it. This solved my issue. – WillB3 Sep 25 '18 at 01:23
  • 1
    You just saved me from spending more frustrating hours trying to eliminate these warnings. Thank you so much. – Waescher Apr 06 '22 at 08:58
  • Stumbled upon this 5 years later. Working on a project targeting 4.6.1 and on VS2019. What i don't understand is what is suggested approach now. Should we use th dll distributed by the Nuget or rely on MSBuild version. I went through the Github links pressented in 'References'. And in one of the Github links, probably a MS guy mentions that the Nuget approach was kind of a temporary thing and we should no longer be using it. Is that correct ? – Dibzmania Feb 08 '23 at 07:17
43

After going through all the solutions presented here and the references cited in this answer, I finally resolved this completely. This is what I believe anyone who experiences this issue should do:

  1. Update all NuGet packages to latest.
  2. Migrate NuGet from packages.config to PackageReference as per the instructions here. Basically, for each project in your solution, In Solution Explorer, right-click on the References node or the packages.config file and select Migrate packages.config to PackageReference.... ASP.NET website projects must remain using packages.config.
  3. Remove any references to System.Net.Http that are not managed by NuGet (for projects using PackageReference, you should see the NuGet symbol enter image description here next to the reference in Solution Explorer). Replace the removed System.Net.Http references with the corresponding NuGet package if you're certain your project requires System.Net.Http (try building without it first). For projects using packages.config, take extra care to ensure that references to System.Net.Http are required and that they are also using NuGet. It may help to remove and re-add System.Net.Http via NuGet anyway (for all projects referencing it), even if already referenced using NuGet. I found that step 2 can cause some disjoint somewhere.
  4. Upgrade to .NET Framework 4.7.2 for the reasons described here. This is part of VS 2019. Otherwise, download it from here or use the Visual Studio Installer for VS 2017.
  5. Remove all the assembly bindings from all app.config and Web.config files then build your solution. app.config bindings are not required anymore. Web.config bindings will be re-added in the next step, but removing them first ensures you don't have any outdated versions in your bindings.
  6. You may now get some other conflicts at this stage. For your ASP.NET website projects, add the binding redirects to your Web.config that are given to you in the warnings. For other .NET Framework applications, for the references that you are getting warnings for, add the corresponding NuGet packages in the projects where you are getting the warnings, even if the project compiles without the reference being added. This forces the project to use the NuGet version and not the local .NET Framework version that might be getting referenced by another package. This is due to cross-over between .NET Framework and .NET Standard as alluded to by rsenna's aforementioned answer. After building, you may need to repeat this step for further references.

If you later find that you get run-time exceptions (even during unit testing) due to manifest mismatches after adding a reference somewhere, remove all the binding redirects from the website project concerned, then re-add the suggested ones given in the warning as per step 6.

I spent a lot of time trying to resolve this issue methodically, so I believe the above steps would fully resolve most people's issues, although some lateral thinking might be required for unusual cases. Let me know if this works (or doesn't work) for you.

Neo
  • 4,145
  • 6
  • 53
  • 76
  • 3
    The [answer you mentioned](https://github.com/dotnet/corefx/issues/22781#issuecomment-376348344) seems to indicate that, in order to avoid (most? all?) binding redirects, we need to upgrade to .NET Framework 4.7.2. I found that quite convincing, so +1 for that. But unfortunately I cannot upgrade, at this moment, our environments to 4.7.2, which makes the `System.Buffers` solution, even if somewhat "random", still the best solution, at least in my case, and at least for now. – rsenna Sep 05 '18 at 08:54
  • 2
    @rsenna Yes, the requirement to upgrade environments to 4.7.2 is a sticking point for us also, but it's just a matter of kicking off the process and cutting through the red tape as usual. I think it's a better long-term solution than short-term hacks. – Neo Sep 05 '18 at 10:18
  • 1
    The System.Buffers solution didn't work for me, but this one did. I would advise to take the time and upgrade because the work arounds that I tried were inconsistent. 90% of the time they would work, but then they would fail and I couldn't figure out why. – JasonlPrice Mar 09 '19 at 15:03
  • @JasonlPrice Thanks. I just had the problem again on an existing ASP.NET website project in VS 2019, and taking the time to upgrade everything using this process fixed it completely. It is noteworthy that specifically for ASP.NET website projects, step 2 doesn't apply, and step 3 has specific instructions for **packages.config** insofar as any required references to `System.Net.Http` are referenced using NuGet and _not_ as regular assembly references. – Neo May 06 '19 at 15:44
  • 2
    This is what worked for me : 1- Deleted file réference to .Net Framework for System.Net.Http 2- Updated framework to 4.7.2 to maximise api surface compatibility of .Net Standard. 3- Updated / consolidated all nugets The bin folder contained System.Net.Http from the framework and not the nuget version so it was crashing at runtime. – Adam Paquette Mar 29 '21 at 16:06
  • 1
    FWIW: We're on .NET 4.6.1 and after migrating packages.config, the version mis-matches were resolved.. – Metro Smurf Jan 03 '22 at 13:23
14

This tends to happen when you have a reference to the framework System.Net.Http, but one of your package references requires the NuGet package System.Net.Http.

See if you have a reference to that assembly, remove it and install the NuGet package instead

erikkallen
  • 33,800
  • 13
  • 85
  • 120
  • 1
    how would i find out which one? – ShaneKm Feb 20 '18 at 09:55
  • after running the uninstall command above, do you still have references to System.Net.Http? – Francesco B. Feb 20 '18 at 10:05
  • This answer resolved my problems with System.Net.Http. I had a real mix of Framework references (4.0.0.0) and package references (4.1.1. 2). Note also that the package manager may still install a version shown as '4.3.3' (Sept 2018) but inside the assembly the Version number is 4.1.1.2. This latter number has to be used in the Web/App config – Rob Von Nesselrode Sep 04 '18 at 04:27
11

There is a new solution to this that works as of the 9th of October 2018.

  1. You will need to update all your references to System.Net.Http to the latest version 4.3.4.
  2. You should install the package into your .Net framework solution that is causing the conflict, even if it does not explicitly require the package.
  3. If your project has the new project structure, edit it and make sure it includes the following package reference:

    <PackageReference Include="System.Net.Http" Version="4.3.4" />
    
  4. Search your solution and delete any existing binding redirects for System.Net.Http they will look as follows

    <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>
    
  5. Rebuild, the warning should now be gone and your code should build and run fine

dahlbyk
  • 75,175
  • 8
  • 100
  • 122
ObiEff
  • 640
  • 8
  • 24
  • This worked for me with a single edit (item 4) to web.config file – PCG May 17 '19 at 14:42
  • 2
    The assembly version for System.Net.Http for Version 4.3.4 is 4.1.1.3. At least for this dll in the Nuget folder "System.Net.Http/4.3.4/lib/net46/System.Net.Http.dll'". – Matt Bussing May 16 '21 at 17:29
6

You can force the version you're installing, so you can have both projects aligned or find a message in the output window, which would be telling you what's wrong or what your dependencies are. Since the official link lists no 4.2 release, I would do this (solution-wide)

Install-Package System.Net.Http -Version 4.1.1

Or for both projects

Get-Project ProjectName | Install-Package System.Net.Http -Version 4.1.1

Or, even better (using the last version)

Install-Package System.Net.Http -Version 4.3.3

EDIT

Apparently you are not the first to experience this. How about the answer here? Basically you can align this section of both projects config file:

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

You might have to adapt the token value. Just in case, could you paste the config file for both projects=

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
  • 2
    still desn't work. even when specifying version number – ShaneKm Feb 19 '18 at 14:49
  • Could you please paste the Output and Package Manager Console output, including those commands above? Thank you. – Francesco B. Feb 19 '18 at 16:02
  • Please see Edit1. Ran this command: Install-Package System.Net.Http -Version 4.3.3 – ShaneKm Feb 20 '18 at 08:45
  • Ok, let's uninstall everything `Get-Project -All | Uninstall-Package System.Net.Http -RemoveDependencies` And then run `Get-Project Project1 | Install-Package System.Net.Http -Version 4.3.3` `Get-Project Project2 | Install-Package System.Net.Http -Version 4.3.3` Of course replace Project1 and Project2 with the right names. Please post the full output of the Package Manager Console, uninstall included. – Francesco B. Feb 20 '18 at 08:58
  • Alright. Uninstall everything again, and this time don't use Nuget but right-click References on your project, select Add, then the .NET version of your framework and System.Net.Http – Francesco B. Feb 21 '18 at 08:05
  • Same thing. I've created a Brand New solution. Added Service Fabric Stateful service. Then simply either via NuGet Console, OR Add New Nuget UI, install System.Net.Http. It will install version 4.1.1.2. Then ADD new Class Library to the same solution, perform same steps. it will add version 4.2.0.0. So Stateful service project, always gets ver 4.1.1.2, and class lib project gets 4.2.0.0 – ShaneKm Feb 21 '18 at 09:02
  • 1
    Problem still persists, however, added this to app.config of Service Fabric project (where ver 4.1.1.2) is installed - warning no longer shows up: – ShaneKm Feb 21 '18 at 09:22
  • Oops, I just wrote the same thing before reading your last comment fully. So is everything ok now? – Francesco B. Feb 21 '18 at 09:46
  • 1
    Well. Still not sure why Service Fabric project will Always force ver 4.1.1.2 to be installed instead of the latest one. – ShaneKm Feb 21 '18 at 10:59
4

The 6 steps that Neo posted above helped me solve my ASP.NET package problems! Thank you Neo! I was dealing with this for over a week.

I just want to share my personal notes from my experience of implementing Neo's post above.

I had an ASP.NET Web API project that targeted .Net Framework 4.6.1

Here is what I did:

  • Upgrade to .Net Framework 4.7.2
  • Update all NuGet packages to the latest.
  • (optionally, could also do "update-package -reinstall" to make sure all packages are associated with 4.7.2)
  • Consolidate packages
  • I did not migrate from packages.config to PackageRefence, because you can't in ASP.NET
  • Removed references from System.Net.Http and others that required it, and added them as NuGet packages.
  • Removed all assembly bindings from web.config and app.config (in .Core, .Tests, .IntegrationTests libraries)
  • Added binding redirects for our in-house NuGet packages that had version numbers that ended with text (n.n.n.n-beta), but removed text and had numbers only (n.n.n.n)
  • Added this to all .csproj files:
<PropertyGroup>
  <AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences>
</PropertyGroup>
  • Make sure the packages in all packages.config files have the same versions in all projects in the solution
    • Make sure the versions are the same in the web.config
    • Make sure the versions are the same in the .csproj file, if applicable
  • Use Microsoft.Net.Compilers 3.1.1 (update all .csproj files in solution, including .Tests and .IntegrationTests)
  • For Data Protection API (DPAPI) that use Redis:
    • Install Microsoft.AspNetCore.DataProtection.StackExchangeRedis 2.2.5
    • StackExchange.Redis 2.0.601
    • Update System.Numerics.Vectors to 4.4.0 (note: 4.5.0 has a bug in it that prevents server connection)
Gary
  • 91
  • 1
  • 5
1

I've had this problem twice now whilst working with Azure Worker roles. Any called to system.net.http simple causes the code to hang, but there is no feedback at all from Azure so it takes hours or days of commenting out code to find the cause let alone a solution.

A simple trick fixed it for me. This isn't my solution, I came across it about 6 months ago, but when the problem happened to me again today, I thought it worth posting.

  1. Delete any references to System.Net.Http from your project
  2. Go to "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib"
  3. Create a new directory, for example "temphide"
  4. Move all System.Net.* DLL's into this directory
  5. Now when you deploy, your project will use the Nuget version, no more conflicts
Steven Elliott
  • 3,214
  • 5
  • 29
  • 40
1

I tried out various solutions (removing the dependentAssembly OR specifying the binding redirect as well). None of them worked.

However, the only solution which worked for me was to explicitly set Specific Version for System.Net.Http (or whatever DLL giving you version issues) to False from Visual Studio.

enter image description here

Umar Topia
  • 241
  • 2
  • 5
1

After spending many days on various projects, trying to fix this recurrent hell: here is a reliable fix that does not break after some time. Tested with 4.6.1 and 4.7.2.

Fix process

Fix your developer machine:

  • Remove all references to the nuget package "System.Net.Http"
  • Open the "%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2" directory (adapt to your specific version).
    • locations may differ on your machine, see this answer
  • In this directory: locate the special file System.Net.Http.dll
  • In this directory: rename "System.Net.Http.dll" to "System.Net.Http.dll.backup"

Do that on all developer machines. This will not break your machine.

In your projects: just add basic "Assembly Reference" for "System.Net.Http".

In your runnable projects (Web/CLI): you might need to set the project reference as "Copy local" to have the correct dll copied when publishing somewhere else.

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

You may add this assembly redirect (app.config/web.config) in some cases:

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>

Why does this work?

It seems the build process is able to see this special file exists. The build will then generate problematic references. This will lead to the exceptions you know well.

Making the file invisible (due to the file extension change) will prevent the build process from doing this crazy stuff.

What is this specific file? Is it special?

This file is fake news. Decompile it, you will see it is empty!
They just put it there to turn us crazy [citation needed].

Locations may vary:

  • when SDK installed:
    • "%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2"
  • without SDK:
    • "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib"

Wait, you can't delete a system file!

That's what I (and others) concluded for some years. This problem continues to come back many times a year. This solution is not as bad as you think, since the problematic file is a FAKE assembly.

Learn more

This dotnet issue on github contains a huge volume of information about the problem. This answer explains the transitive dependency phenomenon that is part of the global problem. This answer suggests deleting the bad dlls.

Oops, it seems I am partially duplicating Steven Elliott's answer. Mine is more detailed though.

SandRock
  • 5,276
  • 3
  • 30
  • 49
-1

Just remove the system.net.http reference from different projects/Class libraries and add the reference again.

Subramanya
  • 19
  • 2
-2

I am working with .NET 4.6.2 and found the same problem. I have two projects a web site and a test project. I review the following:

  • Both project has installed the NuGet package for System.Net.Http

  • The references in both program are equal and both point to the same
    package

The Problem. The Web.Config and the App.Config from the project point to different System.NET.Http.

I substitute the code in both config to get:

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

in both of them. Then the conflict disappear.

freedeveloper
  • 3,670
  • 34
  • 39
  • This `.config` file change is already described at the bottom of [this answer](https://stackoverflow.com/a/48867478/150605) from 2 years ago. – Lance U. Matthews Apr 10 '20 at 02:38