簡體   English   中英

根據.NET標准編譯.NET Framework項目時,為什么缺少此NuGet依賴項?

[英]Why is this NuGet dependency missing when compiling .NET Framework project depending on .NET Standard?

我有一個Visual Studio解決方案,有3個項目。

頂級是.NET Framework 4.6.1控制台應用程序(項目A)。 它取決於.NET Framework 4.6.1類庫(項目B)。 項目B依賴於.NET Standard 2.0類庫(項目C)。

我在Project C中有一些使用System.Data.SqlClient(NuGet包版本4.6.1)的代碼。

由於以下已知問題https://github.com/dotnet/sdk/issues/901我還將System.Data.SqlClient作為NuGet依賴項添加到Project B(.NET Framework類庫)。

這是方案1,當構建解決方案時,System.Data.SqlClient被復制到項目A的/ bin / Debug文件夾,並且應用程序成功運行。

場景1的代碼在這里https://github.com/JamesDunlop/TestDependencyFlowsNetStandard

但是,對於場景2,我現在添加了對項目A的項目引用,以便它現在也直接引用/依賴於項目C(即.NET標准類庫)以及項目B.這模仿了我需要的東西在遺留應用程序中執行。

清潔,重建和運行。 現在,項目A的/ bin / Debug文件夾中缺少System.Data.SqlClient,並且在運行時出現異常“System.IO.FileNotFoundException:'無法加載文件或程序集'System.Data.SqlClient”

為什么System.Data.SqlClient沒有被復制到/ bin / Debug?

請注意,我已選擇不將.NET Framework項目遷移到PackageReferences以解決問題https://github.com/dotnet/sdk/issues/901 ,因為我需要在大型遺留ASP.NET中實現此功能解決方案,這是不可行的。

我希望添加對Project C的引用幾乎沒有效果,除了(如所觀察到的)它會導致更多的類型轉發DLL被復制到/ bin / Debug文件夾。 但我不希望System.Data.SqlClient現在丟失。

我將在此重復我的評論,因為它被認為是有效的答案。

MSBuild日志的構建輸出詳細程度設置為detailed級別,可以提供更多有關所發生情況的見解。

場景1 (引用B,B引用C)

構建日志顯示項目A已成功從項目B的\\bin\\debug文件夾中解析其System.Data.SqlClient依賴項並在本地復制它。
(由於項目B是.NET Framework類庫,其NuGet依賴項會被復制到其bin文件夾。)

Dependency "System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
  Resolved file path is "C:\...\TestDependencyFlows.Library\bin\Debug\System.Data.SqlClient.dll".

場景2 (引用B和C,B引用C)

構建日志提到項目A試圖從NET Standard項目C(和一些知名文件夾)解析其System.Data.SqlClient依賴項,但不再從項目B解析。
(因為項目C是一個NET Standard項目,它不會將其NuGet依賴項復制到其bin文件夾。)
所有這些嘗試都失敗,並顯示該文件在這些位置不存在的消息。

Dependency "System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
  Could not resolve this reference. Could not locate the assembly "System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 
  Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
  For SearchPath "C:\...\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0".
      Considered "C:\...\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.winmd", but it didn't exist.
      Considered "C:\...\TTestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.dll", but it didn't exist.
      Considered "C:\...\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.exe", but it didn't exist.
      ...

解決方案可以是將System.Data.SqlClient NuGet包添加到項目A.

pfx對原始問題的評論基本上回答了這個問題。

在場景1中,我們在構建日志中看到了這一點

3>  Dependency "System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
3>      Resolved file path is "C:\dev\testing\TestDependencyFlowsNetStandard\TestDependencyFlows.Library\bin\Debug\System.Data.SqlClient.dll".

對於場景2,MSBuild不會嘗試使用項目B的bin目錄,並且構建日志會顯示pfx所指示的內容

3> For SearchPath "C:\dev\testing\TestDependencyFlowsNetStandard\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0".
3>          Considered "C:\dev\testing\TestDependencyFlowsNetStandard\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.winmd", but it didn't exist.
3>          Considered "C:\dev\testing\TestDependencyFlowsNetStandard\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.dll", but it didn't exist.
3>          Considered "C:\dev\testing\TestDependencyFlowsNetStandard\TestDependencyFlows.Library.NetStandard\bin\Debug\netstandard2.0\System.Data.SqlClient.exe", but it didn't exist.

此洞察提供了解決方案2的解決方案。

使用本答案中的解決方案,通過將<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>到.NET Standard Project C的.csproj中的<PropertyGroup> ,將System.Data.SqlClient復制到該.NET Standard的bin目錄中然后,項目和MSBuild可以找到並將其復制到最終的構建輸出文件夾。

編輯 - 上面的建議不起作用,因為當運行一個真正的應用程序時,推送到輸出目錄的DLL是錯誤的平台,與.NET Framework不兼容。

作為進一步的信息, pfx引用的構建日志可以在Visual Studio IDE中按照這篇文章獲得

System.Data.SqlClient添加到Project A的pfx建議有效,但這是我想要避免的,因為在真正的單片遺留應用程序中,項目A有許多等價物需要添加(而不是簡單地將其添加到項目B)的下一個級別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM