安装 NuGet 包时已添加具有相同密钥的项 [英] An item with the same key has already been added while Installing NuGet package

查看:24
本文介绍了安装 NuGet 包时已添加具有相同密钥的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用了类库.现在我将该类库作为 NuGet 包,删除类库,当尝试安装包时出现此错误:已添加具有相同密钥的项目"?

In my project, I was using class library. Now I made that class lib as a NuGet package, remove the class lib and when try to install the package this error appears:"An item with the same key has already been added"?

推荐答案

在我的情况下,当我的 packages.config 文件包含不允许的重复包 ID 时,我看到了这个错误.

In my case, I saw this error when my packages.config file contained duplicate package ids which isn't allowed.

您可以使用下面的 PowerShell 脚本来查找解决方案中的所有重复包.它以递归方式查找所有 packages.config 文件,并针对每个 packages.config 文件检查重复的包 ID.

You can use the PowerShell script below to find all duplicate packages in your solution. It finds all packages.config files recursively and per packages.config file, it checks for duplicate package ids.

$solutionFolder = "C:MySolution"
$nugetPackageFile = "packages.config"

$files = Get-ChildItem -Path $solutionFolder -Filter $nugetPackageFile -Recurse

foreach ($file in $files)
{
    [xml]$xml = Get-Content $file.FullName
    $nodes = Select-Xml "/packages/package/@id" $xml
    $packageIds = @{}

    foreach ($node in $nodes) {
        $packageId = $node.Node.'#text'
        try
        {
            $packageIds.Add($packageId, $packageId)
        }
        Catch [System.ArgumentException]
        {
            Write-Host "Found duplicate package in " $file.FullName ". Duplicate package: $packageId"
        }
    }
}

这篇关于安装 NuGet 包时已添加具有相同密钥的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆