gpt4 book ai didi

asp.net-core - 您能否将面向完整框架的包导入到 ASP.NET Core 3+ 应用程序中?

转载 作者:行者123 更新时间:2023-12-04 00:18:25 24 4
gpt4 key购买 nike

我的理解是,从 ASP.NET Core 3.0 开始,.NET Framework 是不受支持的目标框架,因此您只能在 .NET Core 运行时上运行。
如果是这种情况,哪些 NuGet 包可以导入到 ASP.NET Core 3 应用程序中?
我假设您可以引用任何针对 netstandard 的包,但是仅针对完整框架的包(即仅针对 net45 的旧包)呢?
如果您导入的包引用了不属于 .NET Core 的程序集,即 System.Drawing ,会发生什么情况?

最佳答案

TL;DR: 您仍然可以从 .NET Core 3 甚至 .NET 5 引用(依赖的包).NET Framework 程序集,但是如果您调用任何依赖 API 或库的代码,您将收到运行时错误.NET Core 不(还)支持。您可以使用 Microsoft 的 .NET Portability Analyzer 发现这些

背景
首先,你是正确的 ASP.NET Core 3.x applications can no longer target the .NET Framework ,作为 announced by Microsoft in 2018 。该功能以前是 allowed ASP.NET Core applications to call into .NET Framework libraries ,因此为 Web 应用程序迁移到 .NET Core 提供了中间解决方案。

Note: Since the .NET Framework only runs on Windows machines, writing ASP.NET Core web applications which targeted the .NET Framework implicitly restricted those applications to running on Windows.


行为
但是,即使面向 .NET Core 或现在的 .NET 5,您仍然可以引用 .NET Framework 包和程序集,假设您使用的是 Windows 计算机并安装了相应的 .NET Framework。它的内部工作有点复杂,但缺点是 .NET Core 和 .NET 5 将评估 .NET Framework 汇编,就好像它们是 .NET Standard assemblies 一样。如果 API 调用也在 .NET Core 运行时中实现,它会正常工作,但如果 API 调用是 .NET Framework 的一部分,您将收到异常。

Surprise! It's really important to emphasize that this is a runtime exception. You will still be able to reference the .NET Framework assembly, write calls to problematic members, and compile your code without any warnings. But as soon as you call into code dependent on a .NET Framework-specific assembly, you'll receive the runtime exception.


例子
在 .NET 3.0 中,.NET Framework 库的很大一部分已移植到 .NET Core。事实上,这包括您作为示例引用的大多数 System.Drawing 库——尽管有 good reasons you may not want to use them 。但是,如果您深入研究一下,就会发现许多库仍然不受支持。一个明显的例子是 WebConfigurationManager ,它可用于从 web.config 文件访问配置设置。
.NET 框架代码
因此,举个例子,假设您在 .NET Framework 类库中有以下函数,它从 web.config<AppSetting> s 元素返回一个键数组:
public static class Configuration
{
public static string[] GetAppSettings() => System.Web.Configuration.WebConfigurationManager.AppSettings.AllKeys;
}
ASP.NET 核心代码
然后,在 ASP.NET Core Controller 中,公开一个端点来检索此数据:
public class MyController: Controller 
{
public IActionResult ApplicationKeys() => Content(String.Join(", ", Configuration.GetAppSettings()));
}
异常(exception)
在面向 .NET Framework 的 ASP.NET Core 2.x 应用程序中,这将正常工作。但是,在 ASP.NET Core 3.x 或 ASP.NET Core 5 应用程序中,调用 /My/ApplicationKeys/ 路由时会收到以下运行时错误:

System.TypeLoadException: 'Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'


避免意外
如果你和我一样,这会让你非常紧张。当您尝试调用依赖于不受支持的代码的库时,您更愿意收到设计时错误——或者至少是编译时警告。幸运的是,Microsoft 提供了一个 .NET Portability Analyzer ,也就是 also available as a Visual Studio Extension ,正是为了这个目的。
从 .NET 5 开始,SDK 中还内置了一个 compatibility analyzer,它将识别特定平台上 .NET 5 运行时不支持的调用。这要求目标库使用 [SupportedOSPlatform()] 属性显式注释其类型,因此您不会收到有关旧版 .NET Framework 类型的任何警告。但这将有助于识别针对各种平台的库的类似兼容性问题。
例子
例如,如果您在上面的示例代码上运行 Portability Analyzer,它将 output an Excel spreadsheet 标识 T:System.Web.Configuration.WebConfigurationManager is Not Supported in e.g. .NET Core,Version=v3.1.NET Standard + Platform Extensions,Version=v2.0

Note: Microsoft used to offer an API Analyzer as a NuGet package, which promised to provide design-time analysis in Visual Studio. Unfortunately, the code hasn't been updated in two years, and the latest release is 0.2.12-alpha. In my evaluation, it was not effective at identifying issues.


示例项目
我整理了一个 sample project on GitHub 来演示上述行为。它包括以下项目:
  • ASP.NET Core 2.0 Website 针对 .NET Framework 4.8
  • ASP.NET Core 3.1 Website 针对 .NET Core 3.1
  • .NET Framework class library 调用旧版 WebConfigurationManager

  • 两个 ASP.NET Core 网站都包含两个调用同一个 .NET Framework 4.8 类库的端点。第一个是“Hello world”示例,它可以在两个项目上正常执行,因为它完全依赖于通用 API:
    http://localhost:5000/Basic/Index
    第二个将在 ASP.NET Core 3.1 项目上失败,因为它调用了旧的 WebConfigurationManager API:
    http://localhost:5000/Basic/Configuration

    Disclaimer: This is a quick and dirty repository that I put together to verify my understanding prior to posting this. If there's interest, I'll tidy it up and document it. For now, however, it may prove useful for those of you who need to see this in action.


    致谢
    @Chris Pratt 去年提供了 an excellent answer 涵盖类似的 Material 。值得一读。

    关于asp.net-core - 您能否将面向完整框架的包导入到 ASP.NET Core 3+ 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62464366/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com