无法添加NuGet包

时间:2016-05-17 10:42:23

标签: .net visual-studio nuget

我创建了一个NuGet包libtidy,它被推送到Team Services提要。

当我尝试通过NuGet控制台安装时,我收到错误

  

无法添加对'libtidy'的引用

我已阅读此Stack Overflow post,其中OP存在类似问题,但我无法解决此问题 - 我已尝试过:

  • 删除packages文件夹中的所有文件夹并更新
  • 从提升的命令提示符执行regsvr32 "C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\VsLangproj.olb"
  • 从软件包管理器控制台执行Uninstall-Package libtidy -force(由于未安装软件包,这不起作用
  • git clean -dfx
  • 清除nuget缓存
  • 手动删除.nuget目录中的libtidy目录

修改

做了一点研究,这可能与libtidy.dll不是托管代码的事实有关吗?实际上它是ANSI-C。在我们的应用程序中,我们使用TidyManaged作为包装器进行管理,并通过nuget成功安装。目前,如果我们手动将libtidy.dll复制到bin中,它可以正常工作,但如果构建过程在libtidy.dll中拉入,可能会作为Tidymanaged安装的一部分,而目前还没有。

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.dllInstall.ps1放在nuget-libtidy生成的清单中名为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时,我收到以下警告:

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)&amp;在输出窗口中收到以下错误:

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>

2 个答案:

答案 0 :(得分:5)

你可能正在接受这个

  

无法添加对'libtidy'的引用

因为你在lib文件夹里面有libtidy。在此文件夹中安装packag会自动添加一个引用,并且您无法直接添加对非托管库的引用。

如果您还没有,那么您应该考虑手动创建nuget包,而不是通过项目或程序集来完成。要做到这一点:

  1. 创建一个名为nuget-libtidy的文件夹
  2. 将libtidy.dll,TidyManaged.dll以及您需要的任何内容添加到此文件夹
  3. 打开cmd并导航到刚刚创建的文件夹
  4. 运行nuget spec以创建默认清单Package.nuspec(您需要将nuget添加到PATH环境变量中
  5. 将以下xml添加到刚刚在</metadata>结束标记

    之后创建的nuspec文件中
    <files>
        <file src="libtidy.dll" target="content" />
        <file src="TidyManaged.dll" target="lib" />
    </files>
    
  6. 调整您需要的任何其他值。

  7. 您可以将其调用并打包并部署nuget包。您需要将libtidy.dll复制到输出目录。这意味着在安装软件包之后,您必须导航到右键单击visual studio中的dependencies \ libtidy.dll,选择属性并将Copy to Output Directory设置为Copy Always

    如果您不希望每个人都这样做,您可以对nuget-libtidy文件夹和清单文件进行一些调整。基本上你需要做的是创建一个添加

    的Install.ps1文件
    <ItemGroup>
        <Content Include="libtidy.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
    

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

    以下是应该执行所需操作的Install.ps1示例:

    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脚本后,在清单文件中添加另一行:

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

    不要忘记在Windows 10上运行脚本要求您设置执行策略。我建议运行Set-ExecutionPolicy RemoteSigned。在PowerShell中运行此程序后,您将不得不重新启动系统。

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

    修改

    在Install.ps1文件中找到Typo。第3行,$file1 = $project.ProjectItems.Item("libtidy.dll");应为$file = $project.ProjectItems.Item("libtidy.dll";

答案 1 :(得分:0)

我曾经遇到类似的问题,我必须以更高的权限运行VS来安装nuget包。与我们的公司安全设置有关。

相关问题