安装nuget包“已经添加了相同的密钥。”

我正在尝试安装Microsoft.Bcl.Build 1.0.14

努特回归

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. 

这也发生在json.net和其他软件包上。

我可以添加nancy和topshelf,但不能添加json.net和bcl。

我在VS2012和2013中尝试过它。我也尝试卸载nuget并重新安装。 我还尝试将nuget添加到空类库和空控制台应用程序中。 始终返回相同的错误。

这也发生在json.net和其他软件包上。

我也试过没有包文件

任何想法最受赞赏。

这是命令和堆栈跟踪

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

您的堆栈跟踪告诉该故事,它是失败的NuGet.NetPortableProfileTable.BuildPortableProfileCollection() 。 简而言之,它迭代了一组PCL参考assembly配置文件,并且不止一次地遇到相同的配置文件。 这是一个非常强烈的暗示,即c:\program files (x86)\reference assemblies\microsoft\framework\.netportable目录的内容已损坏。

有许多方法可以修复损坏:

  • 如果你有一个积极的回忆来修改这个目录,比如复制文件,那么撤消你所做的。
  • 最安全的方法是卸载所有 Visual Studio版本> = VS2010。 然后手动清理目录,删除仍然存在的任何杂散文件。 然后再次重新安装VS.
  • 不那么安全但速度更快的方法是重命名 c:\ program files(x86)\ reference assemblies目录并安装多目标包 。 我不能保证成功。
  • 您可以尝试自己查找副本并将其删除。 目录结构如下所示:

  Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile 05/14/2014 01:01 PM  Profile1 05/14/2014 01:01 PM  Profile102 05/14/2014 01:01 PM  Profile104 05/14/2014 01:01 PM  Profile131 05/14/2014 01:01 PM  Profile136 05/14/2014 01:01 PM  Profile14 05/14/2014 01:01 PM  Profile143 05/14/2014 01:01 PM  Profile147 05/14/2014 01:01 PM  Profile154 05/14/2014 01:01 PM  Profile158 05/14/2014 01:01 PM  Profile18 05/14/2014 01:01 PM  Profile19 05/14/2014 01:01 PM  Profile2 05/14/2014 01:01 PM  Profile225 05/14/2014 01:01 PM  Profile23 05/14/2014 01:01 PM  Profile24 05/14/2014 01:01 PM  Profile240 05/14/2014 01:01 PM  Profile255 05/14/2014 01:01 PM  Profile3 05/14/2014 01:01 PM  Profile328 05/14/2014 01:01 PM  Profile336 05/14/2014 01:01 PM  Profile344 05/14/2014 01:01 PM  Profile36 05/14/2014 01:01 PM  Profile37 05/14/2014 01:01 PM  Profile4 05/14/2014 01:01 PM  Profile41 05/14/2014 01:01 PM  Profile42 05/14/2014 01:01 PM  Profile46 05/14/2014 01:01 PM  Profile47 05/14/2014 01:01 PM  Profile5 05/14/2014 01:01 PM  Profile6 05/14/2014 01:01 PM  Profile88 05/14/2014 01:01 PM  Profile92 05/14/2014 01:01 PM  Profile95 05/14/2014 01:01 PM  Profile96 Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile 05/14/2014 12:59 PM  . 05/14/2014 12:59 PM  .. 05/14/2014 01:01 PM  Profile111 05/14/2014 01:01 PM  Profile259 05/14/2014 01:01 PM  Profile49 05/14/2014 01:01 PM  Profile7 05/14/2014 01:01 PM  Profile78 Directory of C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.6\Profile 05/14/2014 12:59 PM  . 05/14/2014 12:59 PM  .. 05/14/2014 01:01 PM  Profile151 05/14/2014 01:01 PM  Profile157 05/14/2014 01:01 PM  Profile31 05/14/2014 01:01 PM  Profile32 05/14/2014 01:01 PM  Profile44 05/14/2014 01:01 PM  Profile84 

检查你的packages.config在那里,检查你要安装的软件包。 也许已经有一些参考,这避免了你可以安装新的。

如果您找到引用,请从packages.config中删除引用并尝试再次安装该程序包。

有时我发生在package.config内部是一些旧的引用,即使从Nuget包管理器中删除包后仍然留在那里。

如果没有,您可以删除整个packages.config,然后如果您可以选择让Nuget下载软件包,那么在构建解决方案时,将再次下载软件包。

我希望这有帮助

看起来NuGet正在您的计算机上找到重复的可移植类库(PCL)配置文件。 遗憾的是,密钥未记录在exception调用堆栈中,这会使事情变得简单。

您可以尝试使用Jon Skeet的PclPal程序,它可以列出PCL配置文件并查看是否有任何重复的内容。

我有另一个MonoPcl程序做类似的事情。 它指的是Mono,但适用于Windows。 它使用NuGet源代码,因此它会遇到与您在Visual Studio中看到的问题相同的问题。 但是,您应该能够修改代码以捕获exception或添加一些额外的日志记录,以查看导致问题的重复PCL配置文件。

进入您的packages目录并删除您不再需要的所有版本的包。

然后尝试安装正确的版本。 我认为它正在查看该目录中的所有文件夹,并尝试将它们添加到某种字典/ hashset并找到两个。

只需在工具中更新 Nuget包管理器 – 扩展和更新 – 更新 – 视图工作室库 – 更新Nuget包管理器

我在安装Autofac和Autofac MVC 5集成时遇到了同样的问题。 我使用解决方案中的Manage Nuget Packages解决了这个问题,我取消选中了两个软件包(Uninstall),重新启动了Visual Studio并成功重新安装了软件包。

我刚刚遇到了同样的问题,但安装NuGet Package Manager的最新更新解决了这个问题。

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

发现bin \ Debug文件夹中存在多个Nuget .spec文件。

这在.Net Core和.Net标准可移植类库空间中发生了很多。 即使我没有追溯问题的真正根源,但是当我直接修改project.json依赖项部分时,它似乎最常发生,其中包含许多其他外部依赖项(具有与之关联的术语)。

如果您可以识别负责的库并删除所有依赖项并使用适当的nuget包管理工具来添加包。 必须完全删除并重新创建CLI并不罕见!

还有另一条路径会产生此错误。 以下是如何重现它:

(1)从VS项目(项目1)创建nuget包。
(2)在引用项目1的项目2中,安装项目1 nuget包。
(3)安装将因“相同键”错误而失败。

解决方案是在安装nuget包之前简单地删除对项目1的引用。