简体   繁体   English

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

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

I have a Visual Studio solution, with 3 projects. 我有一个Visual Studio解决方案,有3个项目。

The top level is a .NET Framework 4.6.1 Console App (Project A). 顶级是.NET Framework 4.6.1控制台应用程序(项目A)。 It depends on a .NET Framework 4.6.1 Class Library (Project B). 它取决于.NET Framework 4.6.1类库(项目B)。 Project B depends on a .NET Standard 2.0 Class Library (Project C). 项目B依赖于.NET Standard 2.0类库(项目C)。

I have some code in Project C that uses System.Data.SqlClient (NuGet package version 4.6.1). 我在Project C中有一些使用System.Data.SqlClient(NuGet包版本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). 由于以下已知问题https://github.com/dotnet/sdk/issues/901我还将System.Data.SqlClient作为NuGet依赖项添加到Project B(.NET Framework类库)。

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. 这是方案1,当构建解决方案时,System.Data.SqlClient被复制到项目A的/ bin / Debug文件夹,并且应用程序成功运行。

The code for Scenario 1 is here https://github.com/JamesDunlop/TestDependencyFlowsNetStandard 场景1的代码在这里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 (ie the .NET Standard Class Library), as well as Project B. This mimics what I will need to do in a legacy application. 但是,对于场景2,我现在添加了对项目A的项目引用,以便它现在也直接引用/依赖于项目C(即.NET标准类库)以及项目B.这模仿了我需要的东西在遗留应用程序中执行。

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" 现在,项目A的/ bin / Debug文件夹中缺少System.Data.SqlClient,并且在运行时出现异常“System.IO.FileNotFoundException:'无法加载文件或程序集'System.Data.SqlClient”

Why does the System.Data.SqlClient not get copied to /bin/Debug ? 为什么System.Data.SqlClient没有被复制到/ 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. 请注意,我已选择不将.NET Framework项目迁移到PackageReferences以解决问题https://github.com/dotnet/sdk/issues/901 ,因为我需要在大型遗留ASP.NET中实现此功能解决方案,这是不可行的。

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. 我希望添加对Project C的引用几乎没有效果,除了(如所观察到的)它会导致更多的类型转发DLL被复制到/ bin / Debug文件夹。 But I would not expect System.Data.SqlClient to now be missing. 但我不希望System.Data.SqlClient现在丢失。

I'll repeat my comment above here, as it is considered valid as an answer. 我将在此重复我的评论,因为它被认为是有效的答案。

The MSBuild log, with its build output verbosity set to level detailed , gives more insights in what happens. MSBuild日志的构建输出详细程度设置为detailed级别,可以提供更多有关所发生情况的见解。

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

The build log shows that project A successfully resolved its System.Data.SqlClient dependency from the \\bin\\debug folder of project B and copies it locally. 构建日志显示项目A已成功从项目B的\\bin\\debug文件夹中解析其System.Data.SqlClient依赖项并在本地复制它。
(As project B is a .NET Framework class library, its NuGet dependencies do get copied to its bin folder.) (由于项目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".

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

The build log mentions that project A tries to resolve its System.Data.SqlClient dependency from the NET Standard project C (and some wellknown folders), but not anymore from project B. 构建日志提到项目A试图从NET Standard项目C(和一些知名文件夹)解析其System.Data.SqlClient依赖项,但不再从项目B解析。
(Because project C is a NET Standard project, it doesn't copy its NuGet dependencies to its bin folder.) (因为项目C是一个NET Standard项目,它不会将其NuGet依赖项复制到其bin文件夹。)
All these attempts fail with the message that the file doesn't exist at these locations. 所有这些尝试都失败,并显示该文件在这些位置不存在的消息。

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.
      ...

A solution could be to add the System.Data.SqlClient NuGet package also to project A. 解决方案可以是将System.Data.SqlClient NuGet包添加到项目A.

pfx comment on the original question essentially answers the question. pfx对原始问题的评论基本上回答了这个问题。

In Scenario 1, we see this in the build log 在场景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".

For Scenario 2, MSBuild does not attempt to use the bin directory of Project B and the build log shows this as noted by pfx 对于场景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.

This insight provides the solution to fix Scenario 2. 此洞察提供了解决方案2的解决方案。

Using the solution in this answer , by adding <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to a <PropertyGroup> in the .csproj for the .NET Standard Project C, System.Data.SqlClient is copied to the bin directory of that .NET Standard project and MSBuild is then able to locate and copy it to the final build output folder. 使用本答案中的解决方案,通过将<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>到.NET Standard Project C的.csproj中的<PropertyGroup> ,将System.Data.SqlClient复制到该.NET Standard的bin目录中然后,项目和MSBuild可以找到并将其复制到最终的构建输出文件夹。

EDIT - the suggestion above does not work, since when running a real application the DLL that is pushed to the output directory is for the wrong platform and is incompatible with .NET Framework. 编辑 - 上面的建议不起作用,因为当运行一个真正的应用程序时,推送到输出目录的DLL是错误的平台,与.NET Framework不兼容。

As further info, the build logs that pfx refers to can be obtained within the Visual Studio IDE as per this post 作为进一步的信息, pfx引用的构建日志可以在Visual Studio IDE中按照这篇文章获得

pfx suggestion of adding System.Data.SqlClient to Project A works, but this was something I wanted to avoid because in the real monolithic legacy app there are many equivalents to Project A where it would need to be added (rather than simply adding it to the next level up Project B). System.Data.SqlClient添加到Project A的pfx建议有效,但这是我想要避免的,因为在真正的单片遗留应用程序中,项目A有许多等价物需要添加(而不是简单地将其添加到项目B)的下一个级别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 混合使用.Net Standard 和.Net Framework 时,如何自动部署隐藏的NuGet 依赖项的C# DLL? - How to deploy C# DLL of hidden NuGet dependency automatically when mixing .Net Standard and .Net Framework? 如何将 .Net Standard Nuget 包添加到 .Net 4.5 Framework 项目中? - How to add .Net Standard Nuget package into .Net 4.5 Framework Project? 在NET Standard项目中消耗.NET 4.6.1 NuGet软件包的问题(bin中缺少dll) - Problem consuming .NET 4.6.1 NuGet packages in NET Standard project ( missing dlls in bin ) 为什么在.Net 4.7.2项目中引用.Net标准nuget包会导入很多.Net核心库? - Why referencing a .Net standard nuget package in a .Net 4.7.2 project import a lot of .Net core lib? Visual Studio .NET标准库和.NET Framework项目-NuGet dll清单定义不匹配 - Visual Studio .NET Standard library and .NET Framework project - NuGet dll manifest definition does not match 使用NuGet,PostSharp和Jenkins编译.NET项目 - Compiling a .NET project using NuGet, PostSharp and Jenkins 通过 nuget 与手动参考在 .NET 标准中使用 .NET 框架程序集时的不同错误 - Different errors when using .NET framework assembly in .NET standard via nuget vs manual reference 编译 .NET5 项目时缺少 Microsoft 库 - Microsoft libraries missing when compiling .NET5 project .Net Framework dll无法处理.Net标准项目 - .Net Framework dll not working on .Net Standard project 编译新的.NET Core项目意外依赖项 - Compiling new .NET Core project unexpected dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM