[英]Nuget package does not restore all DLLs and Dependencies
我已经使用Nuget Package Explorer创建了一个Nuget包。 程序包中有一些我在代码中使用的第三方dll。 我的dll包含在Nuget包中,没有在项目参考中直接引用。 Nuget软件包具有以下内容。
- thirdPartyAAA.dll
- thirdPartyAAA.xml (Needed by thirdPartyAAA.dll)
- thirdPartyBBB.dll (needed by thirdPartyAAA.dll
- thirdPartyBBB.dll.config (used by thirdPartyBBB.dll)
- Dependency on HtmlAgilityPack nuget (Needed by thirdPartyAAA.dll)
- Dependency om RestSharp nuget (Needed by thirdPartyAAA.dll)
问题是:当我在代码中引用此Nuget包并编译代码时,我在bin输出文件夹中仅得到aaa.dll。 bin文件夹中缺少以下文件:
- thirdPartyAAA.xml
- thirdPartyBBB.dll
- thirdPartyBBB.dll.config
- All dlls from HtmlAgilityPack nuget
- All dll from RestSharp nuget
在我的代码中,我直接引用thirdPartyAAA.dll。
有没有一种方法-在创建Nuget软件包期间或在引用该软件包时-强制Nuget软件包还原其所有内容及其依赖项? 我需要还原Nuget软件包中包含的所有文件,无论它们是否直接在代码中引用。
谢谢大家的帮助。 如果有帮助,这是包装的清单。
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MyPackage</id>
<version>1.0.0</version>
<title></title>
<authors>Dev</authors>
<owners>Dev</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My package description.</description>
<dependencies>
<dependency id="HtmlAgilityPack" version="1.4.9" />
<dependency id="RestSharp" version="105.0.1" />
</dependencies>
</metadata>
<files>
<file src="content\thirdPartyAAA.chm" target="content\thirdPartyAAA.chm" />
<file src="content\thirdPartyAAA.XML" target="content\thirdPartyAAA.XML" />
<file src="content\thirdPartyBBB.dll.config" target="content\thirdPartyBBB.dll.config" />
<file src="lib\thirdPartyAAA.dll" target="lib\thirdPartyAAA.dll" />
<file src="lib\thirdPartyBBB.dll" target="lib\thirdPartyBBB.dll" />
</files>
</package>
问题是:当我在代码中引用此Nuget包并编译代码时,我在bin输出文件夹中仅得到aaa.dll。 bin文件夹中缺少以下文件:
- thirdPartyAAA.xml
- thirdPartyBBB.dll
- thirdPartyBBB.dll.config
- All dlls from HtmlAgilityPack nuget
- All dll from RestSharp nuget
首先,对于thirdPartyBBB.dll
, 应确保项目的目标框架版本高于dll的目标框架 。 例如,如果项目的目标框架版本是.net 4.6.2,则thirdPartyBBB.dll的目标框架版本应低于.net 4.6.2。 否则,您会将具有较高版本目标框架的dll文件引用到具有较低版本目标框架的项目。 这是不兼容的。
此外,对于此程序包的依赖关系,您可以检查是否将那些依赖关系添加到项目中,并且这些dll的“ 本地复制 ”属性设置为True。 它在我这边工作正常。
其次,对于内容文件,应在程序包的build文件夹中添加一个.targets
文件,并使用以下代码将这些内容文件复制到bin文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(ProjectDir)thirdPartyAAA.chm">
<Link>thirdPartyAAA.chm</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace></CustomToolNamespace>
</None>
<None Include="$(ProjectDir)thirdPartyAAA.XML">
<Link>thirdPartyAAA.XML</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace></CustomToolNamespace>
</None>
<None Include="$(ProjectDir)thirdPartyBBB.dll.config">
<Link>thirdPartyBBB.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace></CustomToolNamespace>
</None>
</ItemGroup>
</Project>
有关一些详细信息,请检查此线程 。
或者,您可以使用Install.ps1
文件更改属性,脚本如下所示:
param($installPath, $toolsPath, $package, $project)
function MarkDirectoryAsCopyToOutputRecursive($item)
{
$item.ProjectItems | ForEach-Object { MarkFileASCopyToOutputDirectory($_) }
}
function MarkFileASCopyToOutputDirectory($item)
{
Try
{
Write-Host Try set $item.Name
$item.Properties.Item("CopyToOutputDirectory").Value = 2
}
Catch
{
Write-Host RecurseOn $item.Name
MarkDirectoryAsCopyToOutputRecursive($item)
}
}
#Now mark everything in the a directory as "Copy to newer"
MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("TheFolderOfYourContentFiles"))
您可以检查类似的问题以获取详细信息。
另外,我创建了一个测试nuget包,您可以检查它是否适合您,并使用目标框架4.6及更高版本的.net framework项目对其进行测试。
https://1drv.ms/u/s!Ai1sp_yvodHf1QJriMiGQWdYveRm
希望这可以帮助。
转到您的解决方案资源管理器,在Ur项目参考上,右键单击bin文件夹中缺少的DLL。 选择属性,然后将“本地复制”设置为true。 编译后,这会将DLL复制到构建路径。
转到您的解决方案资源管理器,在Ur项目参考上,右键单击bin文件夹中缺少的DLL。 选择属性,然后将“本地复制”设置为true。 编译后,这会将DLL复制到构建路径。
要恢复裸露,请参考以下链接
https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore-troubleshooting
我最近遇到了几乎完全相同的问题。
2天后,我发现了几则帖子,澄清了内容文件夹仅在nuget的+ initial +安装期间部署。
这意味着,要像部署项目中的任何其他代码一样,将任何部署为CONTENT的文件都检查到版本控制系统中。
当VS运行“程序包还原”时,就不会像部署项目时那样部署它们,而某些程序包会丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.