NuGet包的从属DLL未复制到输出文件夹

问题描述:

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

我创建的测试解决方案来重现问题:

解决方案ReferenceTest:

  • 项目:SomeLib.dll;引用:
    • MyCompany.Library Nupkg(取决于Newtonsoft.Json,所以这是加得)
    • 起订量Nupkg(取决于Castle.Core,加太)
  • 项目:MyWinFormsApp.exe;项目参考:
    • SomeLib.csproj

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

  • SomeLib.dll
  • Moq.dll
  • Castle.Core.dll
  • MyCompany.Library.dll
  • Newtonsoft.Json.dll

,看起来不错。

但是,当我看着MyWinFormsApp的输出文件夹,Newtonsoft.Json.dll丢失,并在运行应用程序时,它会抛出Newtonsoft.JSON DLL找不到的异常。但是,Castle.Core.dll IS位于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>..\packages\MyCompany.Library.1.0.0\lib\net461\MyCompany.Library.dll</HintPath> 
     <Private>True</Private> 
    </Reference> 
    <Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL"> 
     <HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath> 
     <Private>True</Private> 
    </Reference> 
    <Reference Include="Moq, Version=4.5.28.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL"> 
     <HintPath>..\packages\Moq.4.5.28\lib\net45\Moq.dll</HintPath> 
     <Private>True</Private> 
    </Reference> 
    <Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> 
     <HintPath>..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.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.Library\bin\Release\MyCompany.Library.dll" target="lib\net461"/> 
    <file src="MyCompany.Library\bin\Release\MyCompany.Library.pdb" target="lib\net461"/> 
    <file src="MyCompany.Library\bin\Release\MyCompany.Library.xml" target="lib\net461"/> 
    </files> 
</package> 

我使用Visual Studio 2015年专业。

我的问题:

  1. 为什么Visual Studio的不是抄袭我的依赖DLL?
  2. 为什么要复制Moq的依赖关系?有什么不同?

感谢您的帮助。

Quido

编辑

我一直比较起订量的NUSPEC和我自己的包和(完全绝望了),我发现一个diffence。起订量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>  

我改变了我的依赖性指定targetFramework现在的VisualStudio DOES复制我依赖!

那NuSpec是我改变的唯一的东西,这真的解决了它。我一直在来回地考虑有没有目标框架的情况,结果是一致的(没有目标框架的失败和目标框架的成功)。

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

+0

我发现了关于此主题的另一个问题。 http://stackoverflow.com/questions/1132243/msbuild-doesnt-copy-references-dll-files-if-using-project-dependencies-in-sol它看起来像VS/MSBuild中的错误。这个话题被接受的答案解释得很好。 – Quido

右键点击项目并选择属性“Newtonsoft.json”参考。 在属性页面中检查“Copy Local”属性是否设置为“True”。

+1

在SomeLib.csproj中,复制本地设置为true。这就是为什么它被复制到该项目的输出文件夹。 MyWinFormsApp项目没有对该DLL的引用,所以我无法设置复制本地。 – Quido

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

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

2.为什么要复制Moq的依赖关系?有什么不同?

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

希望这可以帮助你。

+0

感谢您的回复。你所说的“参考污染”,在我看来是“满足依赖性”。该解决方案只需要该组件。没有它就不会开始。我很确定我没有在MyWinFormsApp中添加对Moq的引用。在原始文章中查看我的编辑以获取更多见解... – Quido

+0

不知道您是如何创建测试解决方案的。你是否打包过SomeLib项目,然后将其安装到MyWinFormsApp项目中?或者只是项目参考?我的再现步骤是:创建SomeLib项目 - >添加Moq nuget包 - >创建控制台应用程序项目MyWinFormsApp->将SomeLib项目添加为项目引用MyWinFormsApp项目(不包装SomeLib项目) - >构建MyWinFormsApp项目。与你有什么不同? –