为什么在根据.NET Standard编译.NET Framework项目时缺少此NuGet依赖项?

Jam*_*lop 6 .net c# dll dll-reference .net-standard

我有3个项目的Visual Studio解决方案。

顶层是.NET Framework 4.6.1控制台应用程序(项目A)。它取决于.NET Framework 4.6.1类库(项目B)。项目B依赖于.NET Standard 2.0类库(项目C)。

I have some code in Project C that uses System.Data.SqlClient (NuGet package version 4.6.1).

Due to the following known issue https://github.com/dotnet/sdk/issues/901 I have also added System.Data.SqlClient as a NuGet dependency to Project B (the .NET Framework Class Library).

This is Scenario 1, and when the solution is built, System.Data.SqlClient is copied to the /bin/Debug folder of Project A and the the application runs successfully.

The code for Scenario 1 is here https://github.com/JamesDunlop/TestDependencyFlowsNetStandard

However, for Scenario 2, I now ADD a project reference to Project A such that it now also directly references/depends-on Project C (i.e. the .NET Standard Class Library), as well as Project B. This mimics what I will need to do in a legacy application.

Clean, and rebuild and run. System.Data.SqlClient is now missing from the /bin/Debug folder of Project A, and at run time there is an exception "System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient"

Why does the System.Data.SqlClient not get copied to /bin/Debug ?

Note that I have chosen NOT to migrate the .NET Framework projects to PackageReferences in order to resolve the issue https://github.com/dotnet/sdk/issues/901, since I need to implement this in a large legacy ASP.NET solution where it is not feasible.

I would expect that adding a reference to Project C would have little effect, other than (as observed) it results in a lot more type-forwarding DLLs being copied to the /bin/Debug folder. But I would not expect System.Data.SqlClient to now be missing.

pfx*_*pfx 5

我会在这里重复我上面的评论,因为它被认为是有效的答案。

MSBuild日志中,其设定的水平生成输出的详细程度detailed,给出了会发生什么更多的见解。

场景 1(A 引用 B,B 引用 C)

构建日志显示,项目ASystem.Data.SqlClient\bin\debug项目B的文件夹中成功解析了它的依赖,并将其复制到本地。
(由于项目 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".
Run Code Online (Sandbox Code Playgroud)

场景 2(A 引用 B 和 C,B 引用 C)

构建日志提到项目 A 尝试System.Data.SqlClientNET Standard项目 C(和一些众所周知的文件夹)解析其依赖项,但不再从项目 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.
      ...
Run Code Online (Sandbox Code Playgroud)

一个解决方案可能是将System.Data.SqlClientNuGet 包也添加到项目 A。