2026年8月11日 星期二

[研究]解決 System.ValueTuple 4.5.0 升級 4.6.1 或 4.6.2 問題

[研究]解決 System.ValueTuple 4.5.0 升級 4.6.1 或 4.6.2 問題

2025-08-07

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20.2

NuGet 升級後,Compile 和 Deploy 都正常,Runtime 出錯


xxx

'/' 應用程式中發生伺服器錯誤。

無法載入檔案或組件 'System.ValueTuple, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 或其相依性的其中之一。 系統找不到指定的檔案。

描述: 在執行目前 Web 要求的過程中發生未處理的例外狀況。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

例外狀況詳細資訊: System.IO.FileNotFoundException: 無法載入檔案或組件 'System.ValueTuple, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 或其相依性的其中之一。 系統找不到指定的檔案。

原始程式錯誤:

行 284:        catch (Exception)
行 285:        {
行 286:            throw;
行 287:        }
行 288:    }



堆疊追蹤:

[FileNotFoundException: 無法載入檔案或組件 'System.ValueTuple, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 或其相依性的其中之一。 系統找不到指定的檔案。]
   SixLabors.Fonts.StreamFontMetrics..ctor(TrueTypeFontTables tables) +0
   SixLabors.Fonts.StreamFontMetrics.LoadTrueTypeFont(FontReader reader) +892
   SixLabors.Fonts.FontCollection.AddImpl(Stream stream, CultureInfo culture, FontDescription& description) +92
   ClosedXML.Graphics.DefaultGraphicEngine.AddEmbeddedFont(FontCollection fontCollection) in /_/ClosedXML/Graphics/DefaultGraphicEngine.cs:258
   ClosedXML.Graphics.DefaultGraphicEngine..ctor(String fallbackFont) in /_/ClosedXML/Graphics/DefaultGraphicEngine.cs:69
   ClosedXML.Graphics.<>c.<.cctor>b__31_0() in /_/ClosedXML/Graphics/DefaultGraphicEngine.cs:55
   System.Lazy`1.CreateValue() +243
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
   System.Lazy`1.get_Value() +14443164
   ClosedXML.Excel.XLWorkbook..ctor(LoadOptions loadOptions) in /_/ClosedXML/Excel/XLWorkbook.cs:784
   Admin_Logs.Button_Export_Xlsx_by_ClosedXML_Click(Object sender, EventArgs e) in C:\Git\Solution1\WebApplication1\Admin\Logs.aspx.cs:286
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +138
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +150
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5445


********************************************************************************

根本問題在於 Visual Studio 2019 沒有把 4.6.x 版 System.ValueTuple.dll 拷貝到 bin 目錄 ( 4.5.0 時代會拷貝)


PS C:\Users\Administrator> [System.Reflection.AssemblyName]::GetAssemblyName("D:\Production\OT\Web\bin\System.ValueTuple.dll").FullName System.ValueTuple, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 PS C:\Users\Administrator>


把 .csproj 檔案中


<Reference Include="System.ValueTuple" />

改為


<Reference Include="System.ValueTuple"> <HintPath>..\packages\System.ValueTuple.4.6.2\lib\net47\System.ValueTuple.dll</HintPath> <Private>True</Private> </Reference>

即可。

(完)

相關

沒有留言:

張貼留言