2017-04-06 82 views
5

我遇到了我創建的自定義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

回答

0

右鍵點擊項目並選擇屬性「Newtonsoft.json」參考。 在屬性頁面中檢查「Copy Local」屬性是否設置爲「True」。

+1

在SomeLib.csproj中,複製本地設置爲true。這就是爲什麼它被複制到該項目的輸出文件夾。 MyWinFormsApp項目沒有對該DLL的引用,所以我無法設置複製本地。 – Quido

0

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項目。與你有什麼不同? –

相關問題