[英]Failed to add NuGet package
我創建了一個NuGet軟件包libtidy
,該軟件包已推送到Team Services提要。
當我嘗試通過NuGet控制台安裝時出現錯誤
無法添加對“ libtidy”的引用
我已經閱讀了這篇OP出現類似問題的Stack Overflow帖子 ,但是我無法解決該問題-我已經嘗試過:
regsvr32 "C:\\Program Files (x86)\\Common Files\\microsoft shared\\MSEnv\\VsLangproj.olb"
Uninstall-Package libtidy -force
(此操作無效,因為未安裝軟件包 git clean -dfx
編輯
做一些研究,這可能與libtidy.dll不是托管代碼有關嗎? 實際上是ANSI-C。 在我們的應用程序中,我們使用TidyManaged作為包裝器,該包裝器是受管理的,並已通過nuget成功安裝。 當前,如果我們手動將libtidy.dll復制到bin中,則可以正常工作,但是如果構建過程引入libtidy.dll,則可能會更好,這可能是Tidymanaged安裝的一部分,但目前還不行。
編輯2
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;
編輯3
編輯3 :
我是
a)將libtidy.dll
和Install.ps1
放在nuget spec
生成的清單中名為nuget-libtidy
的目錄中
我有:
<?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)中,並在輸出窗口中出現以下錯誤:
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”的引用
因為您在lib文件夾中的某處有libtidy。 在此文件夾中安裝packag會自動為其添加引用,並且您不能直接添加對非托管庫的引用。
如果尚未安裝,則應考慮手動創建nuget軟件包,而不是通過項目或程序集來進行。 去做這個:
cmd
並導航到您剛剛創建的文件夾 nuget spec
以創建默認清單Package.nuspec
(您需要將nuget添加到PATH
環境變量中 將以下xml添加到</metadata>
關閉標記后剛創建的nuspec文件中
<files> <file src="libtidy.dll" target="content" /> <file src="TidyManaged.dll" target="lib" /> </files>
調整您需要的任何其他值。
您可以將其稱為完成並打包和部署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文件中找到錯別字。 第3行, $file1 = $project.ProjectItems.Item("libtidy.dll");
應該是$file = $project.ProjectItems.Item("libtidy.dll";
我曾經遇到過類似的問題,我必須以較高的權限運行VS來安裝nuget軟件包。 與我們的公司安全設置有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.