遇過兩三次的問題,今天正式找到明確原因,寫成筆記備忘兼分享。

問題發生情境通常為: 使用VS2010開發程式庫(Class Library)專案,為了測試另外加入主控台(Console Application)專案到Solution中,參考該程式庫專案並在Program.cs中寫幾行程式建立類別做測試。

如下圖,Solution中有BooLib程式庫及BooTester主控台程式兩個專案,一開始執行正常,但隨著BooLib不斷加進新功能,忽然間BooTester開始無法編譯,出現找不到BooLib命名空間的編譯錯誤(如下圖左下紅框),眼睜睜看著References明明已參考BooLib(如下圖橘線示意),更何況前一刻還能正常執行和樂融融,VS2010卻翻臉快過翻書,無故裝死說它找不到BooLib,這是哪椿?! (翻桌)

後來才搞清楚,問題出在Console Application預設選用的是.NET Framework 4 Client Profile,與.NET Framework 4(又稱.NET 4 Full )相比,是針對桌面應用程式的瘦身精簡版。因此,當程式庫引用了System.Web、System.Data.OracleClient... 等等.NET 4 Client Profile不包含的類別,VS2010就會因Framework不相容而發生上述無視程式庫的情況。這也能解釋為什麼一開始執行沒問題,等到程式庫愈寫愈大,開始引用System.Web或OracleClient等類別,才開始發出錯誤。另外我也發現,若.NET 3.5專案參考.NET 4專案,也會發生類似找不到程式庫的情形。(但References裡的程式庫項目會有黃色警告,程式裡若未實際引用,編譯時並不會出錯)

未來大家遇到此類狀況,不妨先檢查是否有Framework版本相容問題囉!

順便列出.NET 4 Client Profile與.NET 4 Full的組件差異:

When to use NET4 Client Profile and when to use NET4 Full Framework?

NET4 Client Profile:
Always target NET4 Client Profile for all your client desktop applications (including Windows Forms and WPF apps).

NET4 Full framework:
Target NET4 Full only if the features or assemblies that your app need are not included in the Client Profile. This includes:

  • If you are building Server apps. Such as:
    • ASP.Net apps

    • Server-side ASMX based web services

  • If you use legacy client scenarios. Such as:

    • Use System.Data.OracleClient.dll which is deprecated in NET4 and not included in the Client Profile.

    • Use legacy Windows Workflow Foundation 3.0 or 3.5 (WF3.0 , WF3.5)

  • If you targeting developer scenarios and need tool such as MSBuild or need access to design assemblies such as System.Design.dll

參考來源: http://blogs.msdn.com/b/jgoldb/archive/2010/04/12/what-s-new-in-net-framework-4-client-profile-rtm.aspx


Comments

# by LouisDeng

VS2010對於reference的防呆做得不錯 target是3.5可以reference 2.0的組件,但target是2.0不能reference 3.5的組件 之前從VS2005升級到2010的時候一開始就發生了很多找不到組件的問題 檢查專案檔路徑也都對 後來才發現是VS2010默默地發揮它的作用...避免runtime才出問題

Post a comment