2011/04/11

Reference 中Copy Local = false 造成的問題–Cache Application Block 錯誤

這兩天,在我的專案中將Microsoft Enterprise Library 5.0,移動至GAC。(由於Microsoft Enterprise Library 屬於基底元件,不會常常異動,使用強式名稱並註冊在GAC,整個開發團隊不易有版本上的Issue),結果卻發現Cache Application Block 無法執行了??

看一下錯誤的狀況:
image

這究竟是怎麼一回事呢?怎麼找都找不到錯誤原因,只知道是.NET Reflection 有問題而言,將 Microsoft Enterprise Library 移動到 GAC,整個專案只調整了專案參考中所有對於Microsoft Enterprise Library 的Reference,將其 Reference Properties中的 Copy Local,由True 改成 False。那會不會是這裏出了問題呢?

Copy Local = true | false ,到底有什麼差別呢?這樣來說吧,基本上並沒有太大的差別。當你把Copy Local = true 時,Visual Studio 會主動將你 Reference 的組件複製到 Bin\Debug 的目錄之下,而 Copy Local = false ,則不會。有可能不同的地方在於 Visual Studio 實在太聰明了,它會幫你將 Reference 組件相關 Dependency 的組件也一併複製到這個目錄裏,所以當你 Copy Local = true 時,你會忽略另外一件事,那就是 Reference 組件的Dependency 組件也必須一併加入Reference。因為它都在 Bin\Debug 目錄裏,你的程式不會發生錯誤,你也就不會發現有問題。所以,當你將 Copy Local = false 時,也就有可能會發生異常。

基於這個概念,我重新去看了相關的Reference。基本上要使用 Cache Application Block 必須 Reference 三個組件,其中兩個是Dependency 組件:

  • Microsoft.Practices.EnterpriseLibrary.Caching
  • Microsoft.Practices.EnterpriseLibrary.Common (Dependency Assembly)
  • Microsoft.Practices.ServiceLocation (Dependency Assembly)

後來,發現有些專案沒有Reference Microsoft.Practices.ServiceLocation。加入後,重新執行程式,Cache Applicatoin Block 就沒有發生任何異常了。

0 意見: