为什么在根据 .NET Standard 编译 .NET Framework 项目时会缺少此 NuGet 依赖项? [英] Why is this NuGet dependency missing when compiling .NET Framework project depending on .NET Standard?

查看:27
本文介绍了为什么在根据 .NET Standard 编译 .NET Framework 项目时会缺少此 NuGet 依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个项目的 Visual Studio 解决方案.

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

我在项目 C 中有一些代码使用 System.Data.SqlClient(NuGet 包版本 4.6.1).

由于以下已知问题https://github.com/dotnet/sdk/issues/901 我还添加了 System.Data.SqlClient 作为项目 B(.NET Framework 类库)的 NuGet 依赖项.

这是场景一,在构建解决方案时,将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: 'Could not load file or assembly 'System.Data.SqlClient"

为什么 System.Data.SqlClient 没有被复制到/bin/Debug ?

请注意,我已选择不将 .NET Framework 项目迁移到 PackageReferences 以解决此问题 https://github.com/dotnet/sdk/issues/901,因为我需要在不可行的大型遗留 ASP.NET 解决方案中实现它.

我希望添加对项目 C 的引用几乎没有影响,除了(如观察到的)它会导致更多类型转发 DLL 被复制到/bin/Debug 文件夹.但我不希望 System.Data.SqlClient 现在会丢失.

解决方案

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

MSBuild 日志的构建输出详细程度设置为 detailed 级别,可提供有关所发生情况的更多见解.

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

构建日志显示项目 A 成功解析了项目 B 的 indebug 文件夹中的 System.Data.SqlClient 依赖项,并将其复制到本地.
(由于项目 B 是一个 .NET Framework 类库,它的 NuGet 依赖项确实会被复制到它的 bin 文件夹中.)

依赖System.Data.SqlClient,版本=4.5.0.1,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a".解析的文件路径是C:...TestDependencyFlows.LibraryinDebugSystem.Data.SqlClient.dll".

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

构建日志提到项目 A 尝试从 NET Standard 项目 C(和一些众所周知的文件夹)解析其 System.Data.SqlClient 依赖项,但不再来自项目 B.
(因为项目 C 是一个 NET Standard 项目,它不会将其 NuGet 依赖项复制到其 bin 文件夹中.)
所有这些尝试都失败,并显示文件在这些位置不存在的消息.

依赖System.Data.SqlClient,版本=4.5.0.1,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a".无法解析此引用.无法找到程序集System.Data.SqlClient,Version=4.5.0.1,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a".检查以确保该程序集存在于磁盘上.如果您的代码需要此引用,您可能会遇到编译错误.对于搜索路径C:...TestDependencyFlows.Library.NetStandardinDebug
etstandard2.0".考虑为C:...TestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.winmd",但它不存在.考虑为C:...TTestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.dll",但它不存在.考虑为C:...TestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.exe",但它不存在....

<小时>

解决方案可能是将 System.Data.SqlClient NuGet 包也添加到项目 A.

I have a Visual Studio solution, with 3 projects.

The top level is a .NET Framework 4.6.1 Console App (Project A). It depends on a .NET Framework 4.6.1 Class Library (Project B). Project B depends on a .NET Standard 2.0 Class Library (Project 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.

解决方案

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.

Scenario 1 (A referencing B, B referencing C)

The build log shows that project A successfully resolved its System.Data.SqlClient dependency from the indebug folder of project B and copies it locally.
(As project B is a .NET Framework class library, its NuGet dependencies do get copied to its bin folder.)

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

Scenario 2 (A referencing B and C, B referencing 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.
(Because project C is a NET Standard project, it doesn't copy its NuGetdependencies to its bin folder.)
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.NetStandardinDebug
etstandard2.0".
      Considered "C:...TestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.winmd", but it didn't exist.
      Considered "C:...TTestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.dll", but it didn't exist.
      Considered "C:...TestDependencyFlows.Library.NetStandardinDebug
etstandard2.0System.Data.SqlClient.exe", but it didn't exist.
      ...


A solution could be to add the System.Data.SqlClient NuGet package also to project A.

这篇关于为什么在根据 .NET Standard 编译 .NET Framework 项目时会缺少此 NuGet 依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆