前篇文章看完 ASP.NET Core 3.1 升 5.0 的步驟及注意事項,這篇是我實際升級部落格專案的記錄。

第一步是將 Visual Studio 2019 由 16.5 升級到 16.8。升級後開啟專案,將 Target framework 由 .NET Core 3.1 改成 .NET 5.0:

global.json 由 3.0.100 改成 5.0.100:

{
  "sdk": {
    "version": "5.0.100"
  }
}

其他什麼都沒做,程式一行未改網站就可以跑了。為了怕搞烏龍,我還新增了一小段程式印出 .NET 版本驗證:

public IActionResult Version()
{
    //REF: https://weblog.west-wind.com/posts/2018/Apr/12/Getting-the-NET-Core-Runtime-Version-in-a-Running-Application
    var framework = Assembly.GetEntryAssembly()?
        .GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
    var info = $@"OS: {System.Runtime.InteropServices.RuntimeInformation.OSDescription}
.NET: {framework}
Blog: {Assembly.GetEntryAssembly().GetName().Version}
";
    return Content(info);
}

得證,真的升級成 .NET 5.0 了!

不過,Publish 時出錯,發現是 Publish 的選項忘了改,netcoreapp3.1 也要改成 net50

改完 .pubxml 後,Publish 仍然出現如下錯誤,依 restore 關鍵字感覺與 NuGet 有關:

1>------ Build started: Project: WebEssentials.AspNetCore.OutputCaching, Configuration: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): 
error NETSDK1005: Assets file 'C:\Github\Darkblog.Core\OutputCache\obj\project.assets.json' doesn't have a target for 'netstandard2.1'. 
Ensure that restore has run and that you have included 'netstandard2.1' in the TargetFrameworks for your project.
1>Done building project "WebEssentials.AspNetCore.OutputCaching.csproj" -- FAILED.
2>------ Build started: Project: Darkblog.Core, Configuration: Debug Any CPU ------
2>C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): 
error NETSDK1005: Assets file 'C:\Github\Darkblog.Core\src\obj\project.assets.json' doesn't have a target for 'net5.0'. 
Ensure that restore has run and that you have included 'net5.0' in the TargetFrameworks for your project.
2>Done building project "Darkblog.Core.csproj" -- FAILED.

想起官方文件提過建議要清除 NuGet Cache,所以便執行一次 dotnet nuget locals --clear all,這個指令會清光所有 NuGet 快取:

清除 NuGet 快取再重新編譯,VS 錯誤清單出現一些錯誤,但編譯仍可繼續執行:

上述錯誤應是暫時性的,編譯有順利完成,也成功完成部署,網站各項功能正常(大家讀這篇文章的同時,網站已經是 ASP.NET Core 5.0),升級完成。

補充:我的網站跑 Docker,故還有一項調整是改 Dockerfile,鏡像檔來源由 mcr.microsoft.com/dotnet/core/aspnet:3.1 改為 mcr.microsoft.com/dotnet/aspnet:5.0:

就醬,沒踩到 Breaking Change,一行程式碼都不用改,我的部落格默默由 ASP.NET Core 3.1 升到 5.0。

The experience of upgrading my blog system from ASP.NET Core 3.1 to 5.0.


Comments

Be the first to post a comment

Post a comment