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

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

问题描述

我有一个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复制到Project A的/bin/Debug文件夹中,并且该应用程序成功运行.

方案1的代码在 https://github.com/JamesDunlop/TestDependencyFlowsNetStandard

但是,对于方案2,我现在将对项目A的项目引用添加到项目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 (A引用B,B引用C)

生成日志显示项目A从项目B的 \ bin \ debug 文件夹中成功解析了其 System.Data.SqlClient 依赖关系,并将其本地复制.>(由于项目B是.NET Framework类库,因此它的NuGet依赖项确实会复制到其 bin 文件夹中.)

 依赖性"System.Data.SqlClient,版本= 4.5.0.1,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a".解析的文件路径为"C:\ ... \ TestDependencyFlows.Library \ bin \ Debug \ System.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,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a".无法解决此参考.无法找到程序集"System.Data.SqlClient,版本= 4.5.0.1,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a".检查以确保程序集在磁盘上.如果您的代码需要此引用,则可能会出现编译错误.对于SearchPath"C:\ ... \ TestDependencyFlows.Library.NetStandard \ bin \ Debug \ netstandard2.0".被认为是"C:\ ... \ TestDependencyFlows.Library.NetStandard \ bin \ Debug \ netstandard2.0 \ System.Data.SqlClient.winmd",但它不存在.被视为"C:\ ... \ TTestDependencyFlows.Library.NetStandard \ bin \ Debug \ netstandard2.0 \ System.Data.SqlClient.dll",但它不存在.被视为"C:\ ... \ TestDependencyFlows.Library.NetStandard \ bin \ Debug \ netstandard2.0 \ System.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 \bin\debug 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.Library\bin\Debug\System.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.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.

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

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