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

时间:2017-04-06 14:09:38

标签: c# visual-studio nuget

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

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

Solution ReferenceTest:

  • 项目:SomeLib.dll;引用:
    • MyCompany.Library Nupkg(取决于Newtonsoft.Json,所以也加入了)
    • Moq 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是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 Professional。

我的问题:

  1. 为什么Visual Studio不复制我的依赖dll?
  2. 为什么要复制Moq的依赖项?有什么区别?
  3. 感谢您的帮助。

    Quido

    修改

    我一直在比较Moq的NUSPEC和我自己的包裹(完全绝望),我发现了一个差异。 Moq 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会复制我的依赖项!

    NuSpec是我唯一改变的东西,这真的解决了它。我一直在寻找带有和不带有targetFramework的情况,并且结果是一致的(没有targetFramework的失败以及targetFramework的成功)。

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

2 个答案:

答案 0 :(得分:1)

1.Why is Visual Studio not copying my dependent dll?

Just as you comment, MSBuild doesn't copy references (DLL files) if using project dependencies in solution in order to avoid reference pollution in project SomeLib project. So the references of referenced project will not copy to the output folder.

2.Why is it copying the dependencies of Moq? What's the difference?

As the answer in the first question, the dependencies of Moq should not copy to the output folder of MyWinFormsApp. So you need to check if you have installed the Moq package to the MyWinFormsApp project and you can check the References of MyWinFormsApp, make sure you just only have a project reference of SomeLib project.

Hope this can help you.

答案 1 :(得分:0)

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

相关问题