添加 NuGet 包失败

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

问题描述

我创建了一个 NuGet 包 libtidy,它被推送到团队服务源.

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:Program Files (x86)Common Filesmicrosoft sharedMSEnvVsLangproj.olb"
  • 从包管理器控制台执行 Uninstall-Package 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 Filesmicrosoft sharedMSEnvVsLangproj.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.dllInstall.ps1 放在 nuget 生成的清单中名为 nuget-libtidy 的目录中规格

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 'contentlibtidy.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>
uget-libtidy	oolsInstall.ps1'...
Cannot add a link to the file libtidy.dll. There is already a file of the same name in this folder.
At <path>
uget-libtidy	oolsInstall.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'

因为您在 lib 文件夹中的某处有 libtidy.在此文件夹中安装包会自动添加引用,您不能直接添加对非托管库的引用.

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. </metadata>结束标记之后,将以下xml添加到刚刚创建的nuspec文件中

  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 中右键单击 dependencieslibtidy.dll,选择属性并将 Copy to Output Directory 设置为 Copy Always.

    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 dependencieslibtidy.dll in visual studio, select properties and set Copy to Output Directory to Copy Always.

    如果您不希望每个人都必须这样做,您可以对您的 nuget-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.

    编辑

    在 Install.ps1 文件中发现错字.第 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 包失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
    InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
    quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
    Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
    Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
    how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)