NuGet 包的依赖 DLL 未复制到输出文件夹



我创建的自定义Nuget包遇到了问题。我们称之为MyCompany.Library.nupkg。它托管在公司Artifactory Nuget存储库上。这个包依赖于Newtonsoft.Json。由于某种原因,如果我引用使用该 Nuget 包的项目,则不会将依赖 DLL 复制到项目的输出文件夹中。奇怪的是,当我使用另一个包(例如 Moq,而不是我自己的包)时,会复制依赖的 DLL。

我已经创建了测试解决方案来重现该问题:

解决方案参考测试:

  • 项目:SomeLib.dll; 参考资料:
    • MyCompany.Library Nupkg(取决于Newtonsoft.Json,所以也添加了)
    • Moq Nupkg(取决于Castle.Core,也添加)
  • 项目:MyWinFormsApp.exe; 项目参考:
    • SomeLib.csproj

当我查看SomeLib的输出文件夹时,我看到:

  • SomeLib.dll
  • 最小起订量.dll
  • 城堡核心.dll
  • 我的公司图书馆.dll
  • Newtonsoft.Json.dll

这看起来不错。

但是当我查看MyWinFormsApp的输出文件夹时,Newtonsoft.Json.dll丢失了,并且在运行应用程序时,它会抛出找不到Newtonsoft.Json dll的异常。但是,Castle.Core.dll 位于 MyWinFormsApp 的输出文件夹中。

我已经比较了Moq和MyCompany.Library的nuspecs,我真的找不到任何显着的区别。

我可以更改所有使用我的 SomeLib 来引用 Newtonsoft.Json 的项目,但这是很多项目,我不想为此打扰其他开发人员。他们不应该知道 SomeLib 使用该程序集。

SomeLib.csproj 中的引用是这样的:

<ItemGroup>
<Reference Include="MyCompany.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..packagesMyCompany.Library.1.0.0libnet461MyCompany.Library.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..packagesCastle.Core.3.3.3libnet45Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.5.28.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..packagesMoq.4.5.28libnet45Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..packagesNewtonsoft.Json.8.0.1libnet45Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>

我的 Nuspec 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MyCompany.Library</id>
<version>0.0.0</version>
<description>MyCompany Core library</description>
<authors>MyCompany</authors>
<references>
<reference file="MyCompany.Library.dll" />
</references>
<dependencies>
<dependency id="Newtonsoft.Json" version="[8.0,9.0.1]" />
</dependencies>    
</metadata>
<files>
<file src="MyCompany.LibrarybinReleaseMyCompany.Library.dll" target="libnet461"/>
<file src="MyCompany.LibrarybinReleaseMyCompany.Library.pdb" target="libnet461"/>
<file src="MyCompany.LibrarybinReleaseMyCompany.Library.xml" target="libnet461"/>
</files>
</package>

我使用的是Visual Studio 2015 Professional。

我的问题:

  1. 为什么Visual Studio不复制我的依赖dll?
  2. 为什么要复制 Moq 的依赖项?有什么区别?

感谢您的任何帮助。

基多

编辑

我一直在比较最小起订量的 NUSPEC 和我自己的软件包,(完全绝望)我发现了一个差异。最小起订量Nuspec包含:

<dependencies>
<group targetFramework=".NETFramework4.5">
<dependency id="Castle.Core" version="3.3.3" />
</group>
</dependencies>

我自己的包裹包含:

<dependencies> 
<dependency id="Newtonsoft.Json" version="[8.0,9.0.1]" /> 
</dependencies>     

我更改了我的依赖项以指定目标框架,现在 VisualStudio 确实复制了我的依赖项!

NuSpec是我唯一改变的东西,这确实解决了它。我一直在反复讨论有和没有那个目标框架的情况,结果是一致的(没有目标框架的失败和目标框架的成功)。

所以:问题解决了,但我不知道为什么...

1.为什么Visual Studio不复制我的依赖dll?

正如您评论的那样,如果在解决方案中使用项目依赖项,MSBuild 不会复制引用(DLL 文件),以避免项目 SomeLib 项目中的引用污染。因此,引用项目的引用不会复制到输出文件夹。

2.为什么要复制最小起订量的依赖关系?有什么区别?

作为第一个问题的答案,最小起订量的依赖项不应复制到MyWinFormsApp的输出文件夹中。因此,您需要检查是否已将Moq包安装到MyWinFormsApp项目中,并且可以检查MyWinFormsApp的引用,确保您只有SomeLib项目的项目引用。

希望这能帮助你。

右键单击项目中的"Newtonsoft.json"引用并选择属性。 在属性页中,检查"复制本地"属性是否设置为"True"。

最新更新