【问题标题】:Nuget package does not restore all DLLs and DependenciesNuget 包不会还原所有 DLL 和依赖项
【发布时间】:2018-04-17 18:23:47
【问题描述】:

我使用 Nuget 包资源管理器创建了一个 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>

【问题讨论】:

  • 这个问题有什么更新吗?你解决了这个问题吗?如果没有,请告诉我有关此问题的最新信息吗?

标签: c# visual-studio-2017 nuget nuget-package-restore


【解决方案1】:

问题是:当我在代码中引用这个 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的属性Copy Local是否设置为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>

详细信息请查看this thread

或者,您可以使用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"))

详情可以查看similar issue

另外,我创建了一个测试nuget包,你可以检查它是否适合你,用目标框架4.6及以上的.net框架项目测试它。

https://1drv.ms/u/s!Ai1sp_yvodHf1QJriMiGQWdYveRm

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    转到您的解决方案资源管理器, 在 Ur 项目参考资料上, 右键单击 bin 文件夹中缺少的 DLL。选择属性,然后将“复制本地”设置为真。这将在编译后将 DLL 复制到构建路径。

    【讨论】:

    • 对不起,这不是我要问的。我说的是 Nuget 包并没有像它想象的那样恢复它的一些 dll。
    【解决方案3】:

    转到您的解决方案资源管理器, 在 Ur 项目参考资料上, 右键单击 bin 文件夹中缺少的 DLL。选择属性,然后将“复制本地”设置为真。这将在编译后将 DLL 复制到构建路径。

    nuget 恢复请参考以下链接

    https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore-troubleshooting

    【讨论】:

      【解决方案4】:

      我最近遇到了几乎完全相同的问题。

      2 天后,我发现了几篇帖子,其中阐明了内容文件夹仅在 nuget 的 +initial+ 安装期间部署。

      这意味着部署为 CONTENT 的任何文件都将被检入您的版本控制系统,就像您项目中的任何其他代码一样。

      当 VS 运行“包还原”时,它们不会像您构建项目并且某些包丢失时那样部署。

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 1970-01-01
        • 2020-03-11
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 2012-10-27
        • 2019-03-19
        • 1970-01-01
        相关资源
        最近更新 更多