How to resolve .NET Core package version conflicts
Asked Answered
P

3

25

I am migrating from a .NET MVC 5 Web Application to a .NET Core 2.2 Web API project along with five .NET Standard 2.0 projects all housed under one solution.

I am now receiving 28 warnings (MSB3277) regarding package conflicts, which are all within the System namespace. For example, there appears to be a version conflict for System.Collections.Concurrent between Version=4.0.11.0 and Version=4.0.14.0 (see error block below).

Troubleshooting attempted:

  1. I tried uninstalling all of the .NET Core SDKs that I do not use, and currently have 2.2.300 installed. That did not work.
  2. I tried going into NuGet at the solution level to install the missing assembly, but 4.0.14 isn't even available in the selection list. I can select from the following versions: 4.3, 4.0.12, 4.0.10 and 4.0.0. I did try installing 4.3 in all of the projects, but that did not work.
  3. Other solutions on the web appear to either recommend going down a version on the SDK or hard-coding a reference in the *.csproj file - neither of which I prefer to do if I do not have to. I do not like the former because it seems counter-intuitive to be moving backwards and I do not like the latter because that hard codes a fix to permanently use an older package, which seems like a really bad idea because it seems unlikely that it will update to newer versions that get released.

Does anyone know how to resolve these warnings? Any help is greatly appreciated.

Detailed output on build for one of the warnings:

Line 5419: 3>    Dependency "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5420: 3>        Could not resolve this reference. Could not locate the assembly "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Line 5675: 3>    There was a conflict between "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5675: 3>    There was a conflict between "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5676: 3>        "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
Line 5676: 3>        "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
Line 5677: 3>        References which depend on "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.3\build\netstandard2.0\ref\System.Collections.Concurrent.dll].
Line 5677: 3>        References which depend on "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.3\build\netstandard2.0\ref\System.Collections.Concurrent.dll].

Update

More troubleshooting:

https://github.com/dotnet/standard/issues/731

https://github.com/dotnet/corefx/issues/32561

Is this a Microsoft bug?

Minimal output on build listing all 28 warnings:

3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections.Concurrent" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections.NonGeneric" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.ComponentModel" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Debug" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Process" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Tracing" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.IO.FileSystem" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Linq" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Net.Primitives" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Net.Security" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Resources.ResourceManager" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime.Extensions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime.InteropServices.RuntimeInformation" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Algorithms" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Csp" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Encoding" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Primitives" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.X509Certificates" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Text.Encoding.Extensions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Text.RegularExpressions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.Tasks" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.ThreadPool" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.Timer" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Memory" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
Pauwles answered 16/6, 2019 at 4:28 Comment(10)
Have you checked the web configs for references to these versions?Buttonball
Does this help you youtube.com/watch?v=shn3gL_UJ38? There are time stamps in first comment. At (12:42) is a "Review 3rd party NuGet packages".Babul
@Babul That does not appear to apply considering the project in question does not use any 3rd party NuGet packages. This appears to be a problem with .NET Standard 2.0. Please see my Update that I just posted.Pauwles
Do as the warning says. Set the build output to verbose.Halfassed
@Halfassed If you look at my first code block, you will see that I already did that. The second code block was simply a list of all the items with warnings.Pauwles
ASP.NET Core and ASP.NET MVC have very different architectures. You can't just migrate an existing project. Even the project file system has changed. As for the errors, they are caused because the old project references Full Framework packages either directly or, most likely, indirectly. When you add a .NET Standard 2.0 package to a project targeting a Full Framework version below 4.8, NuGet will also add a bunch of compatibility packages. Those packages aren't needed in ASP.NET Core but .... the old project files don't know that. They think they are top level dependenciesMcnulty
Long story short, you'll have to remove all those packages by hand and then only add back what's needed for a successful compilation. Remove all System.* packages. You may have to do so in a specific order as some packages depend on others, but often, the only reason System.ValueTuples or System.Collections.Concurrent are added is due to compatibility packages.Mcnulty
By that point you realize that it's probably safer to start a new ASP.NET Core project and carefully copy over the code you want to keep, or adapt the code that's already changed, including controllers, pages etcMcnulty
PS: sorry for the bad news but I've been fighting those warnings and worse, failed deployments, for two years. MS itself admitted that retrofitting .NET Standard 2.0 compliance to older frameworks didn't work, meaning the only Full Framework versions that don't have problems each time you upgrade packages are 4.7.2 and 4.8Mcnulty
@PanagiotisKanavos The packages references do not show up in NuGet, so I cannot remove them. However, when I tried to remove the references to them (i.e., using statement), then errors were thrown. So, like you said, I need to remove the reference and replace it with something else. Any idea what that could be? P.S. Instead of the comments, you should post as an answer. Additionally, when I say migrating, I meant that I created all new projects from scratch and copied and pasted the code. I have gone through and fixed what is broken, so it now compiles. But, I have the warnings.Pauwles
S
20

The issue is here strong naming and mixed assembly versions for at least "System.Collections.Concurrent", but probably for all the 28 warnings.

I tried going into NuGet at the solution level to install the missing assembly, but 4.0.14 isn't even available in the selection list.

The versions you are seeing are the NuGet versions, and those aren't the same to the assembly versions. e.g. NuGet version 4.3.0-preview1-24530-04 has assembly version 4.0.13.0 for .NET standard 1.3:

Please note that in the same package, the assembly version for netcore50 is 4.0.10.0!

Is this a Microsoft bug?

No it's a feature. When an assembly is strong named., then the full version should match. If not, this will give a warning. When not using the GAC, then only one version of the assembly could be published - so which one if multiple versions are used? Note- all Microsoft's assemblies are strong named.

The problem here:

  • You are including an (strong named) assembly that's built to System.Collections.Concurrent assembly version 4.0.14.0
  • You are (indirectly) using in your application assembly version 4.0.11.0 for the System.Collections.Concurrent assembly.

So two version for one assembly!

Solutions

There are multiple solutions possible:

  1. Use everywhere the same assembly version for System.Collections.Concurrent (you need to find out which NuGet package is assembly version 4.0.14.0). This is most of the time not feasible.
  2. Install the version 4.0.11.0 and 4.0.14.0 into the GAC - this ins't a really popular option either these days -> Not possible for .NET Core as there is no GAC for .NET Core. See Is there any GAC equivalent for .NET Core?
  3. Use a <bindingRedirect> in your .config. See Redirecting Assembly Versions | Microsoft Docs

    e.g. for "System.Collections.Concurrent":

    <dependentAssembly>
        <assemblyIdentity name="System.Collections.Concurrent" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0-4.0.14.0" newVersion="4.0.11.0" />
    </dependentAssembly>
    

Final notes

There are 28 version issues, but there is a big change that multiple issues are solved with a single bindingRedirect. So the best way is to start with one (preferable the most top-level one), and rebuild and repeat until all are resolved.

Please also note that downgrading an assembly is a bit tricky, e.g the assembly built on 4.0.13.0 could use a feature that is introduced/changed in 4.0.11.0+. So you could also update your version and use that version in "newVersion" attribute

Saraband answered 21/6, 2019 at 15:50 Comment(5)
Thank you for the detailed answer. Question: by applying the bindingRedirect to an older version, does that mean that I will hard code using this older package forever, and thus unable to upgrade to a newer version automatically? If that is the case, will I need to upgrade it manually at some time in the future?Pauwles
This is config and so no hardcoded. When deploying another version of System.Collections.Concurrent - e.g in a release in the future- you need to update the newVersion attribute so the versions match.Saraband
Is it possible to get away from having to do this? Per one of the GitHub links, an individual mentioned that it appears that it won't be addressed until .NET Standard 2.1. In the meantime, they switched over to using .NET Core. I am trying to avoid doing either if possible. But will pursue the former if necessary.Pauwles
You could try adding to your csprojs <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>, but unsure if that will work for this case.Saraband
Yes. I was able to do it with your help. Thank you. The NuGet Package Explorer helped to demystify it for me - I was unaware of that tool and it took some doing to get it installed on Windows 7. At any rate, seeing the differences across the frameworks hinted that The using statements that I copied over were giving it problems. So, I re-migrated the project without the using statements and then re-included them with the help of intellisense.Pauwles
M
0

Binding redirect (bindingRedirect ) for every assembly you can do it manually in config or there is an option in Project file properties. Check it then upgrade.

Misreport answered 24/6, 2019 at 14:42 Comment(0)
D
0

@Julian's solution wasn't working for me. In my case, I had a test project referencing another project with business logic. The dependency graph looked like this:

Project.BL.Tests
↳ Project.BL

Project.BL
↳ Microsoft.Extensions.DependencyInjection.Abstractions Version="6.0.0"
↳ ThirdParty.Nuget.Package

ThirdParty.Nuget.Package
↳ Microsoft.Extensions.DependencyInjection.Abstractions Version="3.1.0"

When I tried to create a ServiceCollection instance in tests, the error was thrown during build time, saying that DI.Abstractions v6 and v3 are conflicting. I tried adding

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

But it didn't fix it.

The solution was to add a ThirdParty.Nuget.Package dependency directly to the test project:

Project.BL.Tests
↳ Project.BL
↳ ThirdParty.Nuget.Package
Dubbin answered 1/12, 2022 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.