28

i am trying to install Microsoft.Bcl.Build 1.0.14

nuget returns

Installing 'Microsoft.Bcl.Build 1.0.14'. Successfully installed 'Microsoft.Bcl.Build 1.0.14'. Adding 'Microsoft.Bcl.Build 1.0.14' to LeadTracker.Calendar. Uninstalling 'Microsoft.Bcl.Build 1.0.14'. Successfully uninstalled 'Microsoft.Bcl.Build 1.0.14'. Install failed. Rolling back... An item with the same key has already been added.

This also happens with json.net and other packages.

I can add nancy and topshelf but not json.net and bcl.

I have tried it in VS2012 and 2013. I have also tried uninstalling nuget and reinstalling. I have also tried adding the nuget to an empty class library and an empty console app. The same error is always returned.

This also happens with json.net and other packages.

I have also tried with no packages file

Any ideas most appreciated.

Here is the command and stacktrace

PM> Install-Package Microsoft.Bcl.Build Installing 'Microsoft.Bcl.Build 1.0.14'. You are downloading Microsoft.Bcl.Build from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device. Successfully installed 'Microsoft.Bcl.Build 1.0.14'. Adding 'Microsoft.Bcl.Build 1.0.14' to GoogleCalendarIntegration. Uninstalling 'Microsoft.Bcl.Build 1.0.14'. Successfully uninstalled 'Microsoft.Bcl.Build 1.0.14'. Install failed. Rolling back... Install-Package : An item with the same key has already been added. At line:1 char:1
+ Install-Package Microsoft.Bcl.Build
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], ArgumentException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPac     kageCommand   PM> $error[0].exception.stacktrace    at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)  at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)    at System.Collections.ObjectModel.KeyedCollection`2.AddKey(TKey key, TItem item)    at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)    at System.Collections.ObjectModel.Collection`1.Add(T item)    at NuGet.CollectionExtensions.AddRange[T](ICollection`1 collection, IEnumerable`1 items)    at NuGet.NetPortableProfileTable.BuildPortableProfileCollection()    at NuGet.NetPortableProfileTable.get_Profiles()    at NuGet.NetPortableProfileTable.GetProfile(String profileName)    at NuGet.NetPortableProfile.Parse(String profileValue, Boolean treatOptionalFrameworksAsSuppor tedFrameworks)    at NuGet.VersionUtility.IsPortableLibraryCompatible(FrameworkName projectFrameworkName, Framew orkName packageTargetFrameworkName)    at NuGet.VersionUtility.IsCompatible(FrameworkName projectFrameworkName, FrameworkName package TargetFrameworkName)    at NuGet.VersionUtility.<>c__DisplayClass8`1.<TryGetCompatibleItems>b__15(IGrouping`2 g)    at System.Linq.Enumerable.WhereListIterator`1.MoveNext()    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)    at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()    at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)   at NuGet.VersionUtility.TryGetCompatibleItems[T](FrameworkName projectFramework, IEnumerable`1  items, IEnumerable`1& compatibleItems)    at NuGet.ProjectSystemExtensions.GetCompatibleItemsCore[T](IProjectSystem projectSystem, IEnum erable`1 items)    at NuGet.ProjectManager.ExtractPackageFilesToProject(IPackage package)    at NuGet.ProjectManager.AddPackageReferenceToProject(IPackage package) at NuGet.ProjectManager.Execute(PackageOperation operation)    at NuGet.ProjectManager.Execute(IPackage package, IPackageOperationResolver resolver)    at NuGet.ProjectManager.AddPackageReference(IPackage package, Boolean ignoreDependencies, Bool ean allowPrereleaseVersions)    at NuGet.VisualStudio.VsPackageManager.<>c__DisplayClass83.<AddPackageReference>b__85() at NuGet.VisualStudio.VsPackageManager.RunProjectAction(IProjectManager projectManager, Action  action)    at NuGet.VisualStudio.VsPackageManager.AddPackageReference(IProjectManager projectManager, IPa ckage package, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)    at NuGet.VisualStudio.VsPackageManager.<>c__DisplayClass3.<InstallPackage>b__7() at NuGet.VisualStudio.VsPackageManager.RunSolutionAction(Action action)    at NuGet.VisualStudio.VsPackageManager.InstallPackage(IProjectManager projectManager, String p ackageId, SemanticVersion version, Boolean ignoreDependencies, Boolean allowPrereleaseVersions, B oolean skipAssemblyReferences, ILogger logger)    at NuGet.VisualStudio.VsPackageManager.InstallPackage(IProjectManager projectManager, String p ackageId, SemanticVersion version, Boolean ignoreDependencies, Boolean allowPrereleaseVersions, I Logger logger)  at NuGet.PowerShell.Commands.InstallPackageCommand.InstallPackage(IVsPackageManager packageMan ager)    at NuGet.PowerShell.Commands.InstallPackageCommand.ProcessRecordCore()    at NuGet.PowerShell.Commands.NuGetBaseCommand.ProcessRecord() PM>
5
  • Have you tried to remove the entry in the package.config file ?
    – christof13
    Jun 26, 2014 at 11:56
  • yes and tried without a packages file. I have updated the question.
    – Jules
    Jun 26, 2014 at 12:06
  • 1
    Try installing the NuGet package from the Package Manager Console window. Then you should be able to see the full exception callstack of the error if you run "$error[0].exception.stacktrace" after the install fails. This might give us a better clue as to why and where it is failing.
    – Matt Ward
    Jun 26, 2014 at 20:37
  • thanks, have added stack trace to question
    – Jules
    Jun 27, 2014 at 9:57
  • Bug reported to NuGet github.com/NuGet/Home/issues/2464 Apr 6, 2016 at 9:48

12 Answers 12

16

Check your packages.config Inside there, check for the packages you are trying to install. Maybe some reference are already there and this avoid that you can install the new ones.

If you find there the references, delete the reference from the packages.config and try to install again the package.

It happened to me sometimes that inside packages.config are some old reference that still left there even after removing the package from the Nuget Package Manager.

If not, you can delete the entire packages.config, then if you have the option to let Nuget download the packages, when you build your solution, the packages will be downloaded again.

I hope this helps

3
  • 1
    phew. so glad this solved my issue and I didn't have to resort to all the fun all the folks have to do with @Hans' answer!
    – GraehamF
    Jul 16, 2016 at 0:59
  • +1 This can easily happen if you manually edit a packages.config - NuGet will refuse to work with a project which has duplicate entries for the same assembly.
    – StuartLC
    Dec 28, 2016 at 10:19
  • Yes I had 2 packages in the package.config with the same id, but with different versions numbers. So I kept the latest version and removed the older version, and then it worked.
    – arame3333
    Jul 21, 2017 at 13:27
14
+350

Your stack-trace tells the tale, it is NuGet.NetPortableProfileTable.BuildPortableProfileCollection() that fails. In a nutshell, it iterates the set of PCL reference assembly profiles and encounters the same profile more than once. This is a very strong hint that the content of your c:\program files (x86)\reference assemblies\microsoft\framework\.netportable directory is corrupted.

There are many possible ways to go about fixing the damage:

  • If you have an active recollection of tinkering with this directory, like copying files, then undo what you did.
  • Safest way to go about it is to uninstall all Visual Studio versions >= VS2010. Then clean up the directory by hand, deleting any stray files that are still present. Then reinstalling VS again.
  • The not-so-safe but speedier way is to rename the c:\program files (x86)\reference assemblies directory and install the multi-targeting pack. I can't promise success.
  • You can try to find the duplicate yourself and remove it. The directory structure looks like this:

 Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile

05/14/2014  01:01 PM    <DIR>          Profile1
05/14/2014  01:01 PM    <DIR>          Profile102
05/14/2014  01:01 PM    <DIR>          Profile104
05/14/2014  01:01 PM    <DIR>          Profile131
05/14/2014  01:01 PM    <DIR>          Profile136
05/14/2014  01:01 PM    <DIR>          Profile14
05/14/2014  01:01 PM    <DIR>          Profile143
05/14/2014  01:01 PM    <DIR>          Profile147
05/14/2014  01:01 PM    <DIR>          Profile154
05/14/2014  01:01 PM    <DIR>          Profile158
05/14/2014  01:01 PM    <DIR>          Profile18
05/14/2014  01:01 PM    <DIR>          Profile19
05/14/2014  01:01 PM    <DIR>          Profile2
05/14/2014  01:01 PM    <DIR>          Profile225
05/14/2014  01:01 PM    <DIR>          Profile23
05/14/2014  01:01 PM    <DIR>          Profile24
05/14/2014  01:01 PM    <DIR>          Profile240
05/14/2014  01:01 PM    <DIR>          Profile255
05/14/2014  01:01 PM    <DIR>          Profile3
05/14/2014  01:01 PM    <DIR>          Profile328
05/14/2014  01:01 PM    <DIR>          Profile336
05/14/2014  01:01 PM    <DIR>          Profile344
05/14/2014  01:01 PM    <DIR>          Profile36
05/14/2014  01:01 PM    <DIR>          Profile37
05/14/2014  01:01 PM    <DIR>          Profile4
05/14/2014  01:01 PM    <DIR>          Profile41
05/14/2014  01:01 PM    <DIR>          Profile42
05/14/2014  01:01 PM    <DIR>          Profile46
05/14/2014  01:01 PM    <DIR>          Profile47
05/14/2014  01:01 PM    <DIR>          Profile5
05/14/2014  01:01 PM    <DIR>          Profile6
05/14/2014  01:01 PM    <DIR>          Profile88
05/14/2014  01:01 PM    <DIR>          Profile92
05/14/2014  01:01 PM    <DIR>          Profile95
05/14/2014  01:01 PM    <DIR>          Profile96

 Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile

05/14/2014  12:59 PM    <DIR>          .
05/14/2014  12:59 PM    <DIR>          ..
05/14/2014  01:01 PM    <DIR>          Profile111
05/14/2014  01:01 PM    <DIR>          Profile259
05/14/2014  01:01 PM    <DIR>          Profile49
05/14/2014  01:01 PM    <DIR>          Profile7
05/14/2014  01:01 PM    <DIR>          Profile78

 Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.6\Profile

05/14/2014  12:59 PM    <DIR>          .
05/14/2014  12:59 PM    <DIR>          ..
05/14/2014  01:01 PM    <DIR>          Profile151
05/14/2014  01:01 PM    <DIR>          Profile157
05/14/2014  01:01 PM    <DIR>          Profile31
05/14/2014  01:01 PM    <DIR>          Profile32
05/14/2014  01:01 PM    <DIR>          Profile44
05/14/2014  01:01 PM    <DIR>          Profile84
6

There is yet another path that produces this error. Here is how to reproduce it:

(1) Create a nuget package from a VS project (project 1).
(2) In project 2 that references project 1, install the project 1 nuget package.
(3) the installation will fail with the "same key" error.

The solution is to simply remove the reference to project 1 before installing the nuget package.

0
5

Looks like NuGet is finding a duplicate Portable Class Library (PCL) profile on your machine. It is shame the key is not logged in the exception callstack which would make things easy.

You could try Jon Skeet's PclPal program which can list the PCL profiles and see if there's anything duplicated.

I have another MonoPcl program that does a similar thing. It refers to Mono but works on Windows. It uses the NuGet source code so it will have the same problem as you are seeing in Visual Studio. However you should be able to modify the code to catch the exception or put some extra logging to see what the duplicate PCL profile is that is causing the problem.

1
  • Thanks your MonoPcl program helped me!!
    – Patrick
    Apr 22, 2016 at 10:09
3

Go into your packages directory and delete all version of the package you don't need anymore.

Then try installing the correct version. I think it is looking at all the folders in that directory and trying to add them to some kind of dictionary/hashset and finding two.

3

Just update the Nuget package manager in tools -- extension and updates -- updates --visual studio gallery --update Nuget package manager

2
  • Thank you for your simple answer!
    – x19
    Jan 1, 2017 at 21:53
  • This worked for me, too. I uninstalled nuget (because there wasn't an update) and re-installed it. This was the only thing that worked for me. Sep 1, 2017 at 12:46
2

I just had a exatly the same problem, but installing the latest update for the NuGet Package Manager solved the problem.

1

I had the same problem installing Autofac and Autofac MVC 5 Integration. I solved this problem using Manage Nuget Packages in the solution, I unchecked both packages (Uninstall), restarted Visual Studio and reinstalled the package successfully.

1

In my case I ran a dotnet restore from the package manager console to get the duplicated key

PM> dotnet restore
  Determining projects to restore...
C:\Program Files\dotnet\sdk\3.1.300\NuGet.targets(128,5): error : An item with the same key has already been added. Key: (XXXX.XXX.XX, 1.0.0.0-preview1) [C:\code\MySolution.sln]
0

I was facing the same problem while Building a project in c#.

System.ArgumentException: An item with the same key has already been added.
     at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
     at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
     at System.IO.Packaging.ZipPackage.ContentTypeHelper.ProcessDefaultTagAttributes(XmlTextReader reader)
     at System.IO.Packaging.ZipPackage.ContentTypeHelper.ParseContentTypesFile(ZipFileInfoCollection zipFiles)
     at System.IO.Packaging.ZipPackage.ContentTypeHelper..ctor(ZipArchive zipArchive, IgnoredItemHelper ignoredItemHelper)
     at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
     at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
     at System.IO.Packaging.Package.Open(Stream stream)
     at NuGet.PackageHelper.GetManifestStream(Stream packageStream)
     at NuGet.OptimizedZipPackage.EnsureManifest()
     at NuGet.OptimizedZipPackage..ctor(String fullPackagePath)
     at NuGet.CommandLine.PackCommand.PrintVerbose(String outputPath)
     at NuGet.CommandLine.PackCommand.BuildPackage(PackageBuilder builder, String outputPath)
     at NuGet.CommandLine.PackCommand.BuildFromNuspec(String path)
     at NuGet.CommandLine.PackCommand.BuildPackage(String path)
     at NuGet.CommandLine.PackCommand.ExecuteCommand()
     at NuGet.CommandLine.Command.ExecuteCommandAsync()
     at NuGet.CommandLine.Command.Execute()
     at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args)

Found that multiple Nuget .spec files were present in bin\Debug folder.

0

This happens in the .Net Core and .Net Standard Portable Class Library space a lot. Even though I have not traced the true root of the problem it seems to happen most when I directly modify the project.json dependencies section with packages that have a lot of other external dependencies (that have terms associated with them).

If you can identify the library responsible and remove all the dependencies and use the proper nuget package management tool to add the packages. It has not uncommon to have to delete and recreate the CLI completely!

0

I had a similar problem. It was corrected when I removed duplicate PackageReference elements with the specified package in the project’s .csproj file using notepad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.