question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build error "An item with the same key has already been added."

See original GitHub issue

“dotnet build” throws an error when project.lock.json contains assemblies with the same name in different packages. While this seems logical on one side, it is quite hard to debug these issues.

My biggest concern is that this is a common problem in Microsoft packages as well. Packages like “Microsoft.Net.Http” and “System.Net.Http” contain assemblies with the same name. AFAIK people should move to the newer “System.Net.Http” package but I’m afraid that’s not always under our control. Some 3rd party libraries might still use “Microsoft.Net.Http” and others might already use “System.Net.Http”.

Steps to reproduce

These are two sample projects that fail:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "warningsAsErrors": true
  },

  "dependencies": {
    "Microsoft.Bcl.Immutable": "1.0.34",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final"
  },

  "frameworks": {
    "net46": { }
  }
}

Error: An item with the same key has already been added. Key: (System.Collections.Immutable, neutral, b03f5f7f11d50a3a)

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "warningsAsErrors": true
  },

  "dependencies": {
    "Hyak.Common": "1.1.0",
    "System.Net.Http.Formatting.Extension": "5.2.3"
  },

  "frameworks": {
    "net46": { }
  }
}

Error: An item with the same key has already been added. Key: (System.Net.Http.Extensions, neutral, b03f5f7f11d50a3a)

Expected behavior

I’m not sure if these are exactly the same libraries and a binding redirect would solve the problem. I’ve seen that there are already some other issues (eg dotnet/sdk#4805 ) on that topic.

If it can’t be solved with a binding redirect, maybe it can be resolved by renaming the libraries?

If this can’t be fixed, how should we proceed in such cases? If the vendor of the library doesn’t change his dependencies we might end up in a blocking situation.

Environment data

dotnet --version output:

.NET Command Line Tools (1.0.0-beta-001718)

Product Information:
 Version:     1.0.0-beta-001718
 Commit Sha:  5b0afe7880

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.10586
 OS Platform: Windows
 Runtime Id:  win10-x86

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:27 (3 by maintainers)

github_iconTop GitHub Comments

23reactions
SergeySagancommented, Nov 16, 2016

I found another solution that seems to fix this problem, do a:

dotnet restore --no-cache
15reactions
hovsepmcommented, Jun 21, 2016

I start hitting this on VS Update 3.

D:\GitHub\azure-sdk-for-net\tools>dotnet --info
.NET Command Line Tools (1.0.0-preview1-002702)

Product Information:
 Version:     1.0.0-preview1-002702
 Commit Sha:  6cde21225e

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.10586
 OS Platform: Windows
 RID:         win10-x64
D:\GitHub\azure-sdk-for-net\tools>NuGet.exe update -self
Checking for updates from https://www.nuget.org/api/v2/.
Currently running NuGet.exe 3.5.0-beta2-1456.
NuGet.exe is up to date.

How to repro:

  1. clone https://github.com/hovsepm/azure-sdk-for-net/
  2. switch to AutoRest branch
  3. open src\ResourceManagement\Authorization\Authorization.sln
  4. build

Actual: Error An item with the same key has already been added. Key: Microsoft.Azure.Test.HttpRecorder TestFramework C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 241

Expected: Build successful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown build error, 'An item with the same key has ...
In question error RG1000: Unknown build error this has happened after a code merge. After removing that duplicate reference, clean the project ...
Read more >
Unknown build error, 'An item with the same key has ...
NET Core 3.1 application and getting this error on first compile after edditing source code, no error if I rebuild it for the...
Read more >
Error "An item with the same key has already been added"
This scenario typically occurs if an operating system image is being used to deploy cluster nodes and that image was not correctly prepared...
Read more >
An Item With the Same Key Has Already Been Added
An item with the same key has already been added dictionary error happens when your project includes a model with the same property...
Read more >
An item with the same key has already been added Error
The error reads: "An item with the same key has already been added." It does not matter if I include the database name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found