組件版本相容問題與版本導向設定是 .NET Framework 單兵戰鬥基本教練的基礎科目(不熟的同學可參考這篇的補充資料連結),若使用 Visual Studio 開發,VS 會在加入參照或安裝 NuGet 套件時自動在 web.config/App.config 加上 <bindingRedirect> 設定導向目前使用的版本,不用費神自己找 publicToken 、確認版號跟手刻 XML,VS 自會打理一切。

但也有例外狀況。我在另個專案再次踩到 System.Memory 4.0.1.1 缺 bindingRedirect 的問題,上回是自己手工補 bindingRedirect 設定,但總覺得應該有更好的辦法。

問了 AI,地表最強的 .NET 開發工具 Visual Studio 沒讓我失望,遇到這種情況 Visual Studio 的錯誤清單(Error List)視窗通常會有版本衝突警示,點兩下則會彈出詢問對話框,貼心問你需不需要幫忙補上 bindingRedirect 設定?Sure, Of Course, Why Not?

原來可以這麼簡單,不愧是頭等艙級的開發體驗,給近年被 VSCode 搶去光彩的 VS 老戰友補個讚!

【後記】一時又忘記組件版號跟檔案版號的查詢方法(瞧我這老人腦),補上 PowerShell 小記加強記憶。

# 查看 DLL 組件版號
[System.Reflection.AssemblyName]::GetAssemblyName('D:\System.Memory.dll').Version
# 查看 DLL 檔案版號
(Get-Item D:\System.Memory.dll).VersionInfo.FileVersion

Visual Studio can automatically fix missing .NET Framework bindingRedirect settings via Error List warnings, saving manual XML edits. Includes PowerShell commands for checking assembly and file versions.


Comments

# by 小黑

謝哥

Post a comment