Quantcast
Channel: 黑暗執行緒
Viewing all articles
Browse latest Browse all 2311

The type 'Global' is defined in an assembly that is not referenced errors in VS2015

$
0
0

[Abastract]

When you open web form aspx source in VS2015, you may get two compilation errors for each aspx:

  • The type 'Global' is defined in an assembly that is not referenced. You must add a reference to assembly 'App_Code.****, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
  • Cannot convert type 'System.Web.HttpApplication' to 'ASP.global_asax'

I found that the code-behind Global.asx.cs is the cause and moving Global.asax.cs logic to Global.asax as inline code can solve the problem. After reporting to Microsoft Connect and getting feedback from support team, I found it is more like a bug of Productivity Power Tools 2015. Reported again.

【問題說明】

Visuals Studio 2015開啟舊Web Form專案,發現奇怪現象,原本專案可以正確編譯,但開啟幾個Web Form ASPX編輯後,網頁功能正常,但每個開啟的ASPX會出現兩個Global.asax相關編譯錯誤:

  • The type 'Global' is defined in an assembly that is not referenced. You must add a reference to assembly 'App_Code.****, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
  • Cannot convert type 'System.Web.HttpApplication' to 'ASP.global_asax'

歸納並爬文後發現,這有點像VS2015的Bug-只要在VS2015開啟編輯WebForm/ASPX,就可能發生以上問題。再做了一些對照實驗,理出一些結論,會不會發生問題跟Global.asax的寫法有關。我弄了一個小專案重現問題:

VS2015開啟Web Site,原本可以正常編譯。

 

開啟Default.aspx,VS2015顯示Default.aspx有CS0012跟CS0030編譯錯誤,但專案編譯跟執行都正常。

問題關鍵在於Global.asax的寫法,舊專案的Global.asax使用Code Behind,程式邏輯寫在App_Code\Global.asax.cs。而新Web Site專案範本預設是直接寫在Global.asax內。將Global.asax.cs的內容搬到Global.asax裡,問題消失!

調整Global.asax改成Inline寫法可以避免這個問題,但如果Global.asax需要使用動用繼承、實做介面等進階技巧時,Code Behind是唯一選擇(延伸閱讀),故這不算完整解決方案。

將問題回報到Microsoft Connect,很快得到微軟Support回覆,建議我改用安全模式測試,對照後才發現問題出在Productivity Power Tools 2015,停用問題便會消失。身為一名盡責的使用者,立刻再將狀況回報到Productivity Power Tools官網,剩下來就靜待修正囉~


Viewing all articles
Browse latest Browse all 2311

Trending Articles