遇到詭異茶包一枚。
同事 O 要新加入同事 D 與我共同開發的一個 Web Site 專案。同事 O 使用 Visual Studio 由 TFS 取得最新版本原始碼,編譯正常,卻在執行偵錯時發生錯誤:
[ArgumentNullException: 值不能為 null。參數名稱: key(英文:Value cannot be null. Parameter name: key)] System.Collections.Generic.Dictionary`2.FindEntry(TKey key) +11772221 System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value) +13 Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.GetLiteralTraceData(LiteralControl literal, TraceData& data) +47 Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.GetTraceData(Object renderedObject) +259 Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject) +35 System.Web.UI.RenderTraceListenerList.EndRendering(TextWriter writer, Object renderedObject) +66 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +170 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +13 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +66 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128 System.Web.UI.HtmlControls.HtmlTableRow.RenderChildren(HtmlTextWriter writer) +47 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +66 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128 System.Web.UI.HtmlControls.HtmlTable.RenderChildren(HtmlTextWriter writer) +47 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +66 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +13 System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +12 System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32 Afa.WebControl.ListAssistant.Render(HtmlTextWriter output) in X:\MySource\ListAssistant.cs:1269
爆炸點在某顆古老的自製 Web Control 元件:
有幾點很可疑:
- 同一專案在同事 O 加入前,在同事 D 與我的電腦執行完全正常。
- 該 Web Control 元件已在正式環境運轉多年,未遇過類似錯誤。
- 爆炸發生在父類別 System.Web.UI.WebControls.WebControl 的 Render(),不是自製程式邏輯,莫非是 .NET Framework 內部的 Bug?
摸不著頭緒之際,為滿足另一系統要求試著將網站掛在 IIS 執行,結果就是正常的!我馬上聯想到:莫非是 Visual Studio 偵錯機制作祟?
BINGO! 在取消 Browser Link 功能後,錯誤瞬間消失!
由以上現象,我大約猜到錯誤原因:當啟用 Browser Link 時,Visual Studio 變成 SignalR 伺服器,並透過 HTTP 模組在每個網頁插入一段 JavaScript 與 SignalR 持續連線,而錯誤 Callstack 出現的 Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener 應是負責在 WebForm 控制項加上標記,以便將瀏覽器網頁上的元素關到 Visual Studio 設計檢視對應的控制項,而這段邏輯與自訂元件的某些行為表現衝突而肇禍。
事後用上述關鍵字爬文,查到不少文章,錯誤訊息都是 Value cannot be null. Parameter name: key,都建議關閉 Brower Link 排除。
結案收工!