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

C# NuGet包的从属DLL未复制到输出文件夹,c#,visual-studio,nuget,C#,Visual Studio,Nuget,我创建的自定义Nuget包有问题。让我们称之为MyCompany.Library.nupkg。它托管在一个企业人工制品Nuget repo上。此包依赖于Newtonsoft.Json。由于某些原因,如果我引用使用该Nuget包的项目,则依赖DLL不会复制到项目的输出文件夹。奇怪的是,当我使用另一个包(例如Moq,而不是我自己的包)时,依赖的DLL被复制 我创建了测试解决方案来重现问题: 解决方案参考测试: 项目:SomeLib.dll;参考资料: MyCompany.Library Nupk

我创建的自定义Nuget包有问题。让我们称之为MyCompany.Library.nupkg。它托管在一个企业人工制品Nuget repo上。此包依赖于Newtonsoft.Json。由于某些原因,如果我引用使用该Nuget包的项目,则依赖DLL不会复制到项目的输出文件夹。奇怪的是,当我使用另一个包(例如Moq,而不是我自己的包)时,依赖的DLL被复制

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

解决方案参考测试:

  • 项目:SomeLib.dll;参考资料:
    • MyCompany.Library Nupkg(依赖于Newtonsoft.Json,因此也添加了此功能)
    • Moq Nupkg(取决于城堡。核心,也添加)
  • 项目: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>
<?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>

..\packages\MyCompany.Library.1.0.0\lib\net461\MyCompany.Library.dll
真的
..\packages\Castle.Core.3.3\lib\net45\Castle.Core.dll
真的
..\packages\Moq.4.5.28\lib\net45\Moq.dll
真的
..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll
真的
我的Nuspec看起来像这样:

  <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>
<?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>

我公司图书馆
0.0.0
MyCompany核心库
我的公司
我正在使用Visual Studio 2015 Professional

我的问题是:

  • 为什么Visual Studio不复制我的依赖dll
  • 为什么要复制Moq的依赖关系?有什么区别
  • 谢谢你的帮助

    奎多

    编辑

    我一直在比较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>     
    
    
    
    我自己的软件包包含:

    <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是我唯一改变的东西,这真的解决了它。我一直在反复讨论有无目标框架的情况,结果是一致的(没有目标框架的失败和目标框架的成功)

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

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

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

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

    2.为什么复制Moq的依赖关系?有什么区别

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


    希望这能对您有所帮助。

    在SomeLib.csproj中,Copy Local设置为true。这就是它被复制到该项目的输出文件夹的原因。MyWinFormsApp项目没有对该DLL的引用,因此我无法将副本设置为本地。我发现了有关此主题的另一个问题。它看起来像是VS/MSBuild中的一个bug。关于这个话题的公认答案解释得很好。谢谢你的回复。你所谓的“参考污染”,在我看来是“令人满意的依赖”。该解决方案只需要该大会。没有它就无法开始。我非常确定我没有在MyWinFormsApp中添加对Moq的引用。请参阅我在原始版本帖子中的编辑,了解更多信息……不确定您是如何创建测试解决方案的。您是否曾经打包SomeLib项目,然后将其安装到MyWinFormsApp项目?还是仅仅是项目参考?我的复制步骤是:创建SomeLib项目->添加Moq nuget包->创建控制台应用程序项目MyWinFormsApp->添加SomeLib项目作为MyWinFormsApp项目的项目引用(不是打包SomeLib项目)->构建MyWinFormsApp项目。你有什么不同吗?