无法添加NuGet包

Sam*_*lly 5 .net visual-studio nuget

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

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

无法添加对"libtidy"的引用

我已阅读此Stack Overflow帖子,其中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;
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

当我跑步时,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.
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

Bra*_*fin 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>
    
    Run Code Online (Sandbox Code Playgroud)
  6. 调整您需要的任何其他值.

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

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

<ItemGroup>
    <Content Include="libtidy.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

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

以下是应该执行所需操作的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;
Run Code Online (Sandbox Code Playgroud)

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

<file src="Install.ps1" target="tools" />
Run Code Online (Sandbox Code Playgroud)

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

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

编辑

在Install.ps1文件中找到错误.第3行$file1 = $project.ProjectItems.Item("libtidy.dll");应该是$file = $project.ProjectItems.Item("libtidy.dll";