![](/img/trans.png)
[英]How to force nuget package reference dlls to be copied to output directory?
[英]Dependent DLLs of a NuGet package not copied to output folder
我遇到了一个我创建的自定义Nuget包的问题。 我们称之为MyCompany.Library.nupkg。 它由企业Artifactory Nuget回购托管。 这个包依赖于Newtonsoft.Json。 由于某种原因,如果我引用使用该Nuget包的项目,则不会将依赖DLL复制到项目的输出文件夹中。 奇怪的是,当我使用另一个包(例如Moq而不是我自己的包)时,依赖的DLL被复制。
我已经创建了测试解决方案来重现问题:
解决方案参考测试:
当我查看SomeLib的输出文件夹时,我看到:
看起来很好。
但是,当我查看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 Professional。
我的问题:
谢谢你的帮助。
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的成功)。
所以:问题解决了,但我不明白为什么......
1.为什么Visual Studio不能复制我的依赖dll?
正如您发表评论一样,如果在解决方案中使用项目依赖项,MSBuild不会复制引用(DLL文件),以避免项目SomeLib项目中的参考污染。 因此引用项目的引用不会复制到输出文件夹。
2.为什么要复制Moq的依赖? 有什么不同?
作为第一个问题的答案,Moq的依赖关系不应该复制到MyWinFormsApp的输出文件夹中。 因此,您需要检查是否已将Moq包安装到MyWinFormsApp项目,并且可以检查MyWinFormsApp的引用,确保您只有SomeLib项目的项目引用。
希望这可以帮到你。
右键单击项目中的“Newtonsoft.json”引用并选择属性。 在属性页中检查“复制本地”属性是否设置为“True”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.