從 ASP.NET Core 2.2 移轉到 3.0

访客 阅读:19 2024-07-13 13:07:19 评论:0
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

作者:Scott AddieRick Anderson

Author: Scott Addie and

本文說明如何將現有的 ASP.NET Core 2.2 專案更新為 ASP.NET Core 3.0。 建立新的 ASP.NET Core 3.0 專案可能有助於:

This paper explains how to update the existing ASP.NET Core 2.2 project to ASP.NET Core 3.0. The creation of a new ASP.NET Core 3.0 project may help:

  • 與 ASP.NET Core 2.2 程式碼比較。
  • 將相關變更複製到您的 ASP.NET Core 3.0 專案。

如果您的解決方案依賴 global.json 檔案而以特定的 .NET Core SDK 版本為目標,請將其 屬性更新為您電腦上安裝的 3.0 版:

If your solution is based on global.json with the specific.NET Core SDK version, please update its properties to the 3.0 version installed on your computer:




ASP.NET Core 3.0 及更新版本只能在 .NET Core 上執行。 請將 目標 Framework Moniker (TFM) 設定為 :

ASP.NET Core 3.0 and updates can only be implemented on.NET Core. Set the target :


不會針對 ASP.NET Core 3.0 產生大量的 NuGet 套件。 這類套件參考應該從您的專案檔中移除。 針對 ASP.NET Core 2.2 Web 應用程式,請考慮下列專案檔:

Do not generate a large number of NuGet packages for ASP.NET Core 3.0. Reference packages of this type should be removed from your project file. For ASP.NET Core 2.2 Web applications, consider the following project files:


ASP.NET Core 3.0 的更新專案檔:

Update project file for ASP.NET Core 3.0:


已更新的 ASP.NET Core 3.0 專案檔:

Updated ASP.NET Core 3.0 project file:

若要查看不再產生的完整套件清單,請選取下列展開清單:

To view the full list of non-generated packages, select the following extension list:

按一下以展開不再產生的套件清單
  • Microsoft.AspNetCore
  • Microsoft.AspNetCore.All
  • Microsoft.AspNetCore.App
  • Microsoft.AspNetCore.Antiforgery
  • Microsoft.AspNetCore.Authentication
  • Microsoft.AspNetCore.Authentication.Abstractions
  • Microsoft.AspNetCore.Authentication.Cookies
  • Microsoft.AspNetCore.Authentication.Core
  • Microsoft.AspNetCore.Authentication.OAuth
  • Microsoft.AspNetCore.Authorization.Policy
  • Microsoft.AspNetCore.CookiePolicy
  • Microsoft.AspNetCore.Cors
  • Microsoft.AspNetCore.Diagnostics
  • Microsoft.AspNetCore.Diagnostics.HealthChecks
  • Microsoft.AspNetCore.HostFiltering
  • Microsoft.AspNetCore.Hosting
  • Microsoft.AspNetCore.Hosting.Abstractions
  • Microsoft.AspNetCore.Hosting.Server.Abstractions
  • Microsoft.AspNetCore.Http
  • Microsoft.AspNetCore.Http.Abstractions
  • Microsoft.AspNetCore.Http.Connections
  • Microsoft.AspNetCore.Http.Extensions
  • Microsoft.AspNetCore.HttpOverrides
  • Microsoft.AspNetCore.HttpsPolicy
  • Microsoft.AspNetCore.Identity
  • Microsoft.AspNetCore.Localization
  • Microsoft.AspNetCore.Localization.Routing
  • Microsoft.AspNetCore.Mvc
  • Microsoft.AspNetCore.Mvc.Abstractions
  • Microsoft.AspNetCore.Mvc.Analyzers
  • Microsoft.AspNetCore.Mvc.ApiExplorer
  • Microsoft.AspNetCore.Mvc.Api.Analyzers
  • Microsoft.AspNetCore.Mvc.Core
  • Microsoft.AspNetCore.Mvc.Cors
  • Microsoft.AspNetCore.Mvc.DataAnnotations
  • Microsoft.AspNetCore.Mvc.Formatters.Json
  • Microsoft.AspNetCore.Mvc.Formatters.Xml
  • Microsoft.AspNetCore.Mvc.Localization
  • Microsoft.AspNetCore.Mvc.Razor
  • Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
  • Microsoft.AspNetCore.Mvc.RazorPages
  • Microsoft.AspNetCore.Mvc.TagHelpers
  • Microsoft.AspNetCore.Mvc.ViewFeatures
  • Microsoft.AspNetCore.Razor
  • Microsoft.AspNetCore.Razor.Runtime
  • Microsoft.AspNetCore.Razor.Design
  • Microsoft.AspNetCore.ResponseCaching
  • Microsoft.AspNetCore.ResponseCaching.Abstractions
  • Microsoft.AspNetCore.ResponseCompression
  • Microsoft.AspNetCore.Rewrite
  • Microsoft.AspNetCore.Routing
  • Microsoft.AspNetCore.Routing.Abstractions
  • Microsoft.AspNetCore.Server.HttpSys
  • Microsoft.AspNetCore.Server.IIS
  • Microsoft.AspNetCore.Server.IISIntegration
  • Microsoft.AspNetCore.Server.Kestrel
  • Microsoft.AspNetCore.Server.Kestrel.Core
  • Microsoft.AspNetCore.Server.Kestrel.Https
  • Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions
  • Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
  • Microsoft.AspNetCore.Session
  • Microsoft.AspNetCore.SignalR
  • Microsoft.AspNetCore.SignalR.Core
  • Microsoft.AspNetCore.StaticFiles
  • Microsoft.AspNetCore.WebSockets
  • Microsoft.AspNetCore.WebUtilities
  • Microsoft.Net.Http.Headers

檢閱中斷性變更

透過上述其中一個套件提供的 ASP.NET Core 功能,可作為 共用架構的一部分。 共用架構是一組安裝在電腦上的組建 (.dll 檔案),並包含執行階段元件和目標套件。 如需詳細資訊,請參閱共用的架構 \(英文\)。

The ASP.NET Core feature provided through one of the above packages can be used as part of a common architecture. shared structure is a set of configurations installed on a computer (.dll file) and contains an active session widget and target package. For more detailed information, please refer to .

  • 以 SDK 為目標的專案會隱含地參考 架構。

    Project with SDK as its target will be consulted implicitly.

    這些專案不需要其他參考:

    These projects do not require any other reference:

    
    
  • 以 或 SDK 為目標的專案,應該將明確 新增至 :

    A project with a target or SDK should be clearly added to:

    
    

使用 Docker 的架構相依組建

使用相依於 ASP.NET Core 共用架構之套件的主控台應用程式架構相依組建,可能會產生下列執行階段錯誤:

Using the configuration of the master control application based on a common set of ASP.NET Core


是包含 ASP.NET Core 執行階段的共用架構,且只存在於 Docker 映像上。 3.0 SDK 會使用 ASP.NET Core 來減少架構相依組建的大小,方法是不包含共用架構中可用的程式庫複本。 這是最多 18 MB 的潛在節省,但是需要 ASP.NET Core 執行階段存在/安裝才能執行應用程式。

It is a shared structure that contains the ASP.NET Core stage and only exists on the Docker image. 3.0 SDK uses ASP.NET Core to reduce the size of the structure by not including a copy of the program library available in the common frame. This is a potential economy of up to 18 MB, but requires the ASP.NET Core stage to exist/installation to run the application.

若要判斷應用程式在 ASP.NET Core 共用架構上是否具有相依性 (直接或間接),請檢查應用程式建置/發佈期間產生的 檔案。 下列 JSON 檔案顯示與 ASP.NET Core 共用架構的相依性:

To determine whether the application is dependent (directly or intermittently) on the ASP.NET Core common structure, check the files generated during the build/ release period of the application. The following JSON files show the dependencies of the ASP.NET Core common setup:


如果您的應用程式使用 Docker,請使用包含 ASP.NET Core 3.0 的基礎映像。 例如: 。

If your application uses Docker, use a base image containing ASP.NET Core 3.0. For example:

ASP.NET Core 3.0 會移除先前屬於 套件參考的一些組件。 若要將已移除的組件視覺化,請比較兩個共用架構資料夾。 例如,比較 2.2.7 版和 3.0.0 版:

ASP.NET Core 3.0 removes some of the components that were previously part of the package reference. To visualize the removed components, please compare two shared frame folders. For example, compare version 2.2.7 with version 3.0.0:

共用架構組件比較

data-linktype=

若要繼續使用已移除組件所提供的功能,請參考對應套件的 3.0 版本:

To continue using the functions provided by the removed component, refer to version 3.0 of the corresponding package:

下圖顯示 ASP.NET Core 2.2 Razor Pages Web 應用程式中已刪除和已變更的行:

The following graph shows the deleted and changed lines in the ASP.NET Core 2.2 Razor Pages Web application:

ASP.NET Core 2.2 Razor Web 應用程式中已刪除和變更的行

ASP.NET Core 2.2 Razor Web application

在上圖中,已刪除的程式碼會以紅色顯示。 已刪除的程式碼不會顯示 cookie 選項程式碼,在比較檔案之前已刪除。

In the above graph, the deleted code is shown in red. The deleted code does not show the cookie option and has been deleted before comparing the file.

下圖顯示 ASP.NET Core 3.0 Razor Pages Web 應用程式中已新增和已變更的行:

The following graph shows new and changed lines in ASP.NET Core 3.0 Razor Pages Web applications:

ASP.NET Core 3.0 Razor Web 應用程式中已新增和變更的行

ASP.NET Core 3.0 Razor Web application

在上圖中,新增的程式碼會以綠色顯示。 如需下列變更的詳細資訊:

In the above graph, the new code is shown in green. The details of the following changes are required:

以 為目標的專案,會隱含地參考先前隨附於 Microsoft.AspNetCore.Mvc.Analyzers 套件的分析器。 不需要其他參考就能啟用這些參考。

The targeted project will implicitly refer to the analyzer previously attached to . These references can be accessed without other references.

如果您的應用程式使用先前隨附於 Microsoft.AspNetCore.Mvc.Api.Analyzers 套件的 API 分析器,請編輯您的專案檔以參考隨附於 .NET Core Web SDK 的分析器:

If your application uses Microsoft.AspNetCore.Mvc. Api. Analyzers package , please edit your ad hoc file for reference to the analyst.NETCoreWeb SDK:



為 MVC 提供 UI 元件的 Razor 類別庫專案,必須在專案檔中設定 屬性:

The Razor library project that provides UI widgets for MVC must configure properties in the project file:



專案在 ASP.NET Core 3.0 或更新版本中預設為內含式裝載模型。 如果專案檔中 屬性的值為 ,您可以選擇性地將其移除。

The project is preset in ASP.NET Core 3.0 or the updated version as . If the property value in the project file is the value, you can choose to remove it.

將 Kestrel 組態移轉至由 () 提供的 Web 主機建立器

Move the Kestrel configuration to Web Host Builder:


如果應用程式以手動方式透過 建立主機,而不是使用 ,請在 Web 主機建立器上呼叫 :

If the application creates the host manually instead of using it, call on the web host builder:



連線配接器 () 已從 Kestrel 中移除。 以連線中介軟體取代連線配接器。 連線中介軟體類似於 ASP.NET Core 管線中的 HTTP 中介軟體,但是適用於較低層級的連線。 HTTPS 和連線記錄:

Connector () has been removed from Kestrel. The connection broker software is replaced by the connection broker software. The connection broker software is similar to the HTTP intermediary software in the ASP.NET Core line, but is suitable for lower-level connections. HTTPS and connection logs:

  • 已從連線配接器移至連線中介軟體。
  • 這些擴充方法可在舊版 ASP.NET Core 中運作。

如需詳細資訊,請參閱 Kestrel 文章中 ListenOptions.Protocols 一節中的 TlsFilterConnectionHandler 範例

For more detailed information, please refer to ListenOptions.Protocols section .

Kestrel 傳輸層已公開為 中的公用介面。 作為這些更新的一部分:

The Kestrel transfer layer has been made public as a public interface. As part of these updates:

如需詳細資訊,請參閱下列 GitHub 資源:

For more information, see the following GitHub resources:

針對以舊版 ASP.NET Core 為目標的應用程式:

An application aimed at the old version of ASP.NET Core:

  • Kestrel 將 HTTP/1.1 區塊化尾端標頭新增至要求標頭集合。
  • 尾端會在要求本文讀取至結尾之後提供。

這引起了一些關於標頭和尾端之間模棱兩可的顧慮,因此在 3.0 中尾端已移至新的集合 ()。

This gives rise to a number of ambiguous concerns between the cap and the end and has been moved to a new assembly (at the middle end of 3.0).

HTTP/2 要求尾端如下:

HTTP/2 requires the following endpoint:

  • ASP.NET Core 2.2 中無法使用。
  • 在 3.0 中以 的形式提供。

有新的要求擴充方法來存取這些尾端。 使用 HTTP/1.1,尾端會在要求本文讀取至結尾之後提供。

There are new requirements to expand access to these tails. Using HTTP/1.1, the end will be provided after the request has been read to the end.

針對 3.0 版本,有下列 方法可供使用:

For version 3.0, the following methods are available:

  • :取得要求 標頭,其中列出在本文之後預期有哪些尾端。
  • :指出要求是否支援接收尾端標頭。
  • :檢查要求是否支援尾端,以及是否可供讀取。 此檢查不會假設有尾端可供讀取。 即使此方法傳回 ,也可能不會有尾端可讀取。
  • :從回應取得所要求的尾端標頭。 在呼叫 之前檢查 ,否則如果要求不支援尾端標頭,則可能會發生 NotSupportedException

如需詳細資訊,請參閱將要求尾端放在個別集合中 (dotnet/AspNetCore #10410)

For more information, please refer to .

啟用或停用同步 I/O API,例如 、 和 。 這些 API 是導致應用程式當機的執行緒耗盡來源。 在 3.0 中,預設會停用 。 如需詳細資訊,請參閱 Kestrel文章中的同步 I/O 一節

Enables or disables synchronisation I/ O API, e. g., and these APIs are the sources that cause the application to run out of energy. In 3.0, the default will be deactivated. See Kestrel's article Synchronization I/ O section .

如果需要同步 I/O,可以在所使用的伺服器上設定 選項來啟用 (呼叫 時,例如若使用 Kestrel)。 請注意,伺服器 (Kestrel、HttpSys、TestServer 等等) 都有不會影響其他伺服器的 選項。 您可以使用 選項,針對每個要求啟用所有伺服器的同步 I/O:

If you want to sync I/O, set the options to be enabled on the server you use (calling, e. g. if Kestrel is used). Note that all servers (Kestrel, HttpSys, TestServer, etc.) have options that do not affect other servers. You can use the options to synchronize every request to enable all servers I/O:


如果您在 Dispose 中呼叫同步 API 的 TextWriter 實作或其他資料流發生問題,請改為呼叫新的 DisposeAsync API。

If you call API at , call API at , please refer to a new

如需詳細資訊,請參閱 [公告] 在所有伺服器中停用 AllowSynchronousIO (dotnet/AspNetCore #7644)

For more information, please refer to on all servers.

Newtonsoft.JsonXmlSerializerDataContractSerializer 型輸出格式器僅支援同步序列化。 為了允許這些格式器使用伺服器的 AllowSynchronousIO 限制,MVC 會在寫入磁碟之前,緩衝這些格式器的輸出。 由於緩衝處理,MVC 會在使用這些格式器回應時包含 Content-Length 標頭。

,

System.Text.Json 支援非同步序列化,因此 型格式器不會緩衝處理。 請考慮使用此格式器來改善效能。

若要停用緩衝處理,應用程式可以在其啟動程序中設定 SuppressOutputFormatterBuffering

To deactivate buffers, the application can set


請注意,如果未設定 ,這可能會導致應用程式擲回執行階段例外狀況。

Please note that, if not set, this may lead to application throwing back the phase exception.

在 ASP.NET Core 2.1 中,Microsoft.AspNetCore.Server.Kestrel.Https.dll 的內容已移至 Microsoft.AspNetCore.Server.Kestrel.Core.dll。 這是使用 屬性的非中斷性更新。 針對 3.0,空白 Microsoft.AspNetCore.Server.Kestrel.Https.dll 組件和 NuGet 套件已移除。

In ASP.NET Core 2.1, the contents of Microsoft.AspNetCore.Server.Kestrel.Https.dll have been moved to .AspNetCore.Server.Kestrel.Core.dll. This is a non-intermittal update using attributes. For 3.0, the blanks Microsoft.AspNetCore. Server.Kestrel.Htps.dll and the NuGet package have been removed.

參考 Microsoft.AspNetCore.Server.Kestrel.Https 的程式庫應該將 ASP.NET Core 相依性更新為 2.1 會更新版本。

See Microsoft. AspNetCore. Server. Kestrel. Https should update ASP.NET Cores to 2.1.

以 ASP.NET Core 2.1 或更新版本為目標的應用程式或程式庫,應該移除對 Microsoft.AspNetCore.Server.Kestrel.Https 套件的任何直接參考。

Applications or libraries targeting ASP.NET Core 2.1 or updated versions should remove any direct reference to packages.

改善 ASP.NET Core 共用架構的過程中,Newtonsoft.Json (Json.NET) 已從 ASP.NET Core 共用架構移除。

In the process of , has been removed from the ASP.NET Core joint venture.

適用於 ASP.NET Core 的預設 JSON 序列化程式現在是 System.Text.Json,這是 .NET Core 3.0 中的新功能。 請考慮盡可能使用 。 它是高效能的,不需要額外的程式庫相依性。 不過,由於 是新功能,因此目前可能缺少您的應用程式所需的功能。 如需詳細資訊,請參閱如何從 Newtonsoft.Json 移轉至 System.Text.Json

The default JSON sequencing program suitable for ASP.NET Core is now , which is a new feature in https://docs.microft. com/zh-tw/dotnet/site/stunaridation. However, since this function is new, it may currently lack the functionality required for your app.

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
关注我们

若遇到问题,加微信客服---清歌

搜索
排行榜
扫一扫,加我为微信好友加我为微信好友