![](/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.