简体   繁体   English

NuGet 包在 a.Net Standard 2.0 dll 被.Net Framework 和.Net Core 使用

[英]NuGet packages in a .Net Standard 2.0 dll used by both .Net Framework and .Net Core

Setup设置

Say I have a.Net Standard 2.0 class library project and I add a Nu NuGet package that is compatible with.Net Standard 2.0 to it.假设我有一个.Net Standard 2.0 class 库项目,我添加了一个与.Net Standard 2.0 兼容的 Nu NuGet package。

I then reference that class library project from both a.Net Framework console project and a.Net Core console project.然后我从 a.Net Framework 控制台项目和 a.Net Core 控制台项目中引用 class 库项目。

To restate with a picture:用图片重述:

NuGet 通过 DLL

Question问题

How does each of the console applications deal with getting the right NuGet code for their type of application?每个控制台应用程序如何处理为其应用程序类型获取正确的 NuGet 代码?

Notes笔记

Note: I tried this using Microsoft.Extensions.DependencyInjection, and it works fine in the.Net Core 3.1 console app, but throws a "File Not Found" exception in the.Net Framework 4.7.2 console app (looking for the Dependency Injection DLL).注意:我使用 Microsoft.Extensions.DependencyInjection 进行了尝试,它在 .Net Core 3.1 控制台应用程序中运行良好,但在 .Net Framework 4.7.2 控制台应用程序中抛出“找不到文件”异常(寻找依赖注入DLL)。 This leads me to believe that.Net Standard 2.0 NuGets are really.Net Core NuGets...这让我相信 .Net Standard 2.0 NuGets 真的是 .Net Core NuGets...

Note to the Note: I am trying to understand what happens here, not fix the "File Not Found" issue.注意注意事项:我试图了解这里发生的情况,而不是解决“找不到文件”问题。 (That is easily fixable by referencing the Microsoft.Extensions.DependencyInjection NuGet in the.Net Framework 4.7.2 console app). (这很容易通过在 .Net Framework 4.7.2 控制台应用程序中引用 Microsoft.Extensions.DependencyInjection NuGet 来解决)。

In your scenario two different package resolution startegies are used.在您的场景中,使用了两种不同的package 分辨率 startegies There is the old way of managing packages with packages.config and the new way with PackageReferences .使用packages.config管理包的旧方法和使用PackageReferences的新方法。 There is also the old project format of .NET Framework projects and the new SDK-style format that was introduced for .NET Core, but is also usable in .NET Framework applications.还有 .NET 框架项目的旧项目格式和为 .NET 核心引入的新SDK 样式格式,但也可用于 .NET 框架应用程序。

With the old project format that is still used by most existing .NET Framework applications regardless of packages.config or PackageReference , the .NET Framework Console application is only able to access the class library, but not the assemblies of its referenced NuGet package, because it is not a direct reference but via your library, hence indirect. With the old project format that is still used by most existing .NET Framework applications regardless of packages.config or PackageReference , the .NET Framework Console application is only able to access the class library, but not the assemblies of its referenced NuGet package, because it is不是直接参考,而是通过您的图书馆,因此是间接的。 This is als called a transitive dependency.这也称为传递依赖。

In the new SDK-style project format with PackageReference , this is fundamentally different.在带有PackageReference的新 SDK 样式项目格式中,这是根本不同的。 There, transitive dependencies are possible.在那里,传递依赖是可能的。 This means, that your .NET Framework console application can access the class library project, as well as its referenced assemblies via the NuGet package.这意味着,您的 .NET 框架控制台应用程序可以访问 class 库项目,以及通过 NuGet ZEFE90A8E604A7C8B61DZ 库项目及其引用的程序集。

The SDK-style format with PackageReference is the default for .NET Core projects, so they support transitive dependencies out of the box.带有PackageReference的 SDK 样式格式是 .NET 核心项目的默认格式,因此它们支持开箱即用的传递依赖项。 Only with the old project format you have to add NuGet packages manually, because it cannot access the transitive dependency through the class library.只有使用旧项目格式,您必须手动添加 NuGet 包,因为它无法通过 class 库访问传递依赖。 You can migrate existing .NET Framework projects to the new SDK-style format, to enable the same behavior as in .NET Core.您可以将现有的 .NET 框架项目迁移到新的 SDK 样式格式,以启用与 .NET Core 中相同的行为。

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

相关问题 更新到.NET Standard 2.0 Nuget软件包 - Updating to .NET Standard 2.0 Nuget packages 搜索.NET Core或标准NuGet包 - Search for .NET Core or Standard NuGet packages 在 .NET core 2.0 中引用 .NET framework 4 dll - referencing .NET framework 4 dll in .NET core 2.0 我可以使用在.NET Standard 2.0中为.NET Framework编写的包吗? - Can I use packages written for .NET Framework in .NET Standard 2.0? 以编程方式恢复 nuget 包(.net core & .net framework) - Restore nuget packages Programmatically (.net core & .net framework) 用本地 dll 文件替换 asp.net core NuGet 包 - Replace asp.net core NuGet packages with local dll files ASP.NET:由Nuget引起的.NET Framework /标准/核DLL冲突。 “你必须添加对程序集System.Runtime的引用......” - ASP.NET: .NET Framework/Standard/Core DLL collisions, caused by Nuget. “You must add a reference to assembly System.Runtime…” Python .Net未加载.Net Standard 2.0 dll - Python .Net Not loading .Net Standard 2.0 dll .Net Standard 2.0 生成 NuGet package 包括同一解决方案中的项目和 NuGet 包 - .Net Standard 2.0 Generate NuGet package including projects in the same solution and NuGet packages 适用于CRM REST的.NET Standard 2.0 Nuget软件包 - .NET Standard 2.0 Nuget package for CRM REST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM