添加NuGet包失败 [英] Failed to add NuGet package

查看:135
本文介绍了添加NuGet包失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个NuGet包 libtidy ,该包已推送到Team Services提要中。

I have created a NuGet package libtidy which is pushed to a Team Services feed.

尝试通过NuGet控制台进行安装时出现错误

When I try installing via the NuGet console I get the error


失败添加对'libtidy'的引用

Failed to add reference to 'libtidy'

我已阅读此堆栈溢出帖子,其中OP也存在类似问题,但我无法解决该问题-我已经尝试过:

I have read this Stack Overflow post where the OP has a similar problem but I have been unable to resolve the issue - I've tried:


  • 删除包文件夹中的所有文件夹并更新

  • 执行 regsvr32 C: \程序文件(x86)\公用文件\Microsoft共享\MSEnv\VsLangproj.olb 从提升的命令提示符下

  • 执行从软件包管理器控制台卸载软件包libtidy -force (由于未安装软件包,此操作不起作用

  • git clean -dfx

  • 清除nuget缓存

  • 手动删除.nuget目录中的libtidy目录

  • Deleting all the folders inside of the packages folder and updating
  • Executing regsvr32 "C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\VsLangproj.olb" from an elevated command prompt
  • Doing Uninstall-Package libtidy -force from the package manager console (this doesn't work as the package isn't installed
  • git clean -dfx
  • clearing nuget cache
  • manually removing the libtidy directory in the .nuget directory

编辑

做了一些研究,可能与libtidy.dll有关是不是托管代码?实际上是ANSI-C。在我们的应用程序中,我们使用TidyManaged作为包装器,该包装器是受管理的,并已通过nuget成功安装。目前,如果我们手动将libtidy.dll复制到bin中,则可以正常工作,但是如果构建过程将libtidy.dll拉入(最好是作为Tidymanaged安装的一部分,则最好不要这样做)会更好。

Having done a little research, could this be to do with the fact that libtidy.dll is not managed code? Infact it is ANSI-C. In our application we are using TidyManaged, as a wrapper, which is managed, and is successfully installed via nuget. Currently if we manually copy in libtidy.dll into bin, it works fine, but it would be better if the build process pulled in libtidy.dll, perhaps as part of the Tidymanaged install, which it does not at present.

EDIT2

param($installPath, $toolsPath, $package, $project)

$file = $project.ProjectItems.Item("libtidy.dll");

If ($file -eq $null)
{
     $project.ProjectItems.AddFromFile("libtidy.dll");
     $file = $project.ProjectItems.Item("libtidy.dll");
}

$file.Properties.Item("CopyToOutputDirectory").Value = [int]1;

EDIT3

编辑3

我是

a)放置 libtidy.dll Install.ps1 在由<$生成的清单中名为 nuget-libtidy 的目录中c $ c> nuget spec

a) placing libtidy.dll and Install.ps1 in a directory called nuget-libtidy in the manifest generated by nuget spec

我有:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>nuget-libtidy</id>
    <version>1.0.0</version>
    <authors>Name</authors>
    <owners>Name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>nuget-libtidy</description>
    <copyright>Copyright 2016</copyright>
    <tags>nuget-libtidy</tags>
  </metadata>
  <files>
      <file src="libtidy.dll" target="content" />
      <file src="Install.ps1" target="tools" />
  </files>
</package>

当我运行 nuget pack 时,以下警告:

When I run nuget pack I get the following warning:

WARNING: 1 issue(s) found with package 'nuget-libtidy2'.

Issue: Assembly outside lib folder.
Description: The assembly 'content\libtidy.dll' is not inside the 'lib' folder and hence it won't be added as reference when the package is installed into a project.
Solution: Move it into the 'lib' folder if it should be referenced.

b)构建应用程序时,libtidy.dll放置在项目的根目录中(而不是bin)&在输出窗口中出现以下错误:

b) When we build the application libtidy.dll is placed in the root of the project (not the bin) & get the following error in the output window:

Added package 'nuget-libtidy' to 'packages.config'
Executing script file <path>\nuget-libtidy\tools\Install.ps1'...
Cannot add a link to the file libtidy.dll. There is already a file of the same name in this folder.
At <path>\nuget-libtidy\tools\Install.ps1:7 char:5
+     $project.ProjectItems.AddFromFile("libtidy.dll");
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Successfully installed 'nuget-libtidy' to <Namepace>


推荐答案

您可能会得到


无法添加对 libtidy的引用

Failed to add reference to 'libtidy'

libtidy在lib文件夹中的某个位置。在此文件夹中安装packag会自动为其添加引用,并且您不能直接添加对非托管库的引用。

because you have libtidy somewhere inside the lib folder. Installing a packag in this folder will automatically have a reference added to it, and you can't add a reference to an unmanaged library directly.

如果尚未添加,您应该研究手动创建nuget软件包,而不是通过项目或程序集来进行。为此:

If you are not already, you should look into manually creating your nuget package rather than doing it by project or assembly. To do this:


  1. 创建一个名为nuget-libtidy的文件夹

  2. 添加libtidy.dll,TidyManaged .dll以及您需要在此文件夹中执行的其他操作

  3. 打开 cmd 并导航至您刚刚创建的文件夹

  4. 运行 nuget spec 创建默认清单 Package.nuspec (您需要添加nuget到您的 PATH 环境变量

  5. 将以下xml添加到刚在< /元数据> 结束标记

  1. Create a folder called nuget-libtidy
  2. Add libtidy.dll, TidyManaged.dll and anything else you need into this folder
  3. Open cmd and navigate to the folder you just created
  4. Run nuget spec to create the default manifest Package.nuspec (you will need to have nuget added to your PATH environment variable
  5. Add the following xml to the nuspec file just created right after the </metadata> closing tag

<files>
    <file src="libtidy.dll" target="content" />
    <file src="TidyManaged.dll" target="lib" />
</files>


  • 调整您需要的任何其他值。

  • Adjust any of the other values you need.

    您可以将其称为完成并打包和部署nuget软件包,需要将libtidy.dll复制到输出目录。在该程序包中,您必须导航以右键单击Visual Studio中的dependency\libtidy.dll,选择属性,然后将复制到输出目录设置为始终复制

    You could call it done and pack and deploy the nuget package. You need libtidy.dll to be copied to the output directory. This means after you install the package, you would have to navigate to right-click on dependencies\libtidy.dll in visual studio, select properties and set Copy to Output Directory to Copy Always.

    如果您不希望每个人都必须这样做,则可以对数字进行一些调整, libtidy文件夹和清单文件。基本上,您需要做的是创建一个Install.ps1文件,该文件添加

    If you don't want everyone to have to do this you could make a few more adjustments to you nuget-libtidy folder and the manifest file. Basically you need to do is create an Install.ps1 file that adds

    <ItemGroup>
        <Content Include="libtidy.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
    

    到已安装项目的项目文件。

    to the project file of the installed project.

    以下是Install.ps1的示例,它可以完成您想要的操作:

    Here is an example of Install.ps1 that should do what you want:

    param($installPath, $toolsPath, $package, $project)
    
    $file = $project.ProjectItems.Item("libtidy.dll");
    
    If ($file -eq $null)
    {
        $project.ProjectItems.AddFromFile("libtidy.dll");
        $file = $project.ProjectItems.Item("libtidy.dll");
    }
    
    $file.Properties.Item("CopyToOutputDirectory").Value = [int]1;
    

    PowerShell脚本完成后,在清单文件中添加另一行:

    Once your PowerShell script is done add another line to you manifest file:

    <file src="Install.ps1" target="tools" />
    

    别忘了在Windows 10上运行脚本需要您设置执行策略。我建议运行 Set-ExecutionPolicy RemoteSigned 。在PowerShell中运行此程序后,必须重新启动系统。

    Don't forget running scripts on Windows 10 requires you to set the execution policy. I would suggest running Set-ExecutionPolicy RemoteSigned. After you run this in PowerShell, you will have to reboot your system.

    此时,您应该能够打包和部署。

    At this point, you should be able to pack and deploy.

    EDIT

    Typo。第3行, $ file1 = $ project.ProjectItems.Item( libtidy.dll); 应该为 $ file = $ project.ProjectItems.Item ( libtidy.dll;

    Typo found in Install.ps1 file. Line 3, $file1 = $project.ProjectItems.Item("libtidy.dll"); should be $file = $project.ProjectItems.Item("libtidy.dll";

    这篇关于添加NuGet包失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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