最近發現一個三年前的一個 ASP.NET Web API 專案沒有設定 CI 自動建置,在設定的過程中發現無法建置方案 (Build Solution),錯誤訊息是找不到 .NET Framework 4.5 參考組件,但我的專案明明是 .NET Framework 4.8 了啊,經過一番查找之後,才發現原來是資料庫專案(Database Project)搞的鬼,這篇文章我就來說說這個問題的來龍去脈與解法。
問題描述
首先,我們先來看看 Azure Pipelines 在建置過程中顯示的錯誤訊息:
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1229,5): Error MSB3644: The reference assemblies for .NETFramework,Version=v4.5 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks
查看更細部的執行過程紀錄,可以看到以下內容:
##[error]C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1229,5): Error MSB3644: The reference assemblies for .NETFramework,Version=v4.5 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks Project "D:\a\1\s\src\backend\MY_Quotation.sln" (1) is building "D:\a\1\s\src\backend\MY_Database\MY_Database.sqlproj" (4) on node 1 (default targets). C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1229,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.5 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [D:\a\1\s\src\backend\MY_Database\MY_Database.sqlproj] _CleanRecordFileWrites: Creating directory "obj\Release". Done Building Project "D:\a\1\s\src\backend\MY_Database\MY_Database.sqlproj" (default targets) -- FAILED. Done Building Project "D:\a\1\s\src\backend\MY_Quotation.sln" (default targets) -- FAILED.
雖然大致可以看出疑似跟 MY_Database.sqlproj
有關,但第一時間沒想到為什麼資料庫專案會用到 .NET Framework 4.5 呢?我這個專案並沒有在資料庫專案用到 SQLCLR 啊!
解決方案
我查了一下資料庫專案的專案屬性,切換到 SQLCLR 的地方,這才發現原來 Target framework 被設定到了 .NET Framework 4.5,即便你的專案沒用到 SQLCLR,在建置的時候依然會參考 .NET Framework 4.5 的組件,而這就是專案無法建置的原因!
當我切換到 .NET Framework 4.8 之後,專案就可以順利的完成 CI 自動建置了!👍
相關連結