組件繫結重新導向功能測試

  在開發多個專案下,共用的功能大部份都會寫在共用的函式庫內,而在函式庫引用的外部組件 如:iTextSharp、NPOI等,若與其他專案上的版本不一致的話,會造成
AP無法執行,在不想讓現有的專案再去重新參考較新或較舊的組件的話,可以考慮使用BindingRedirect來解決。

測試案例:AP與Lib各自參考iTextSharp
Case 1: AP參考版本較低、Lib參考版本較高
執行結果:在AP使用Lib的方法會出現找不到版本較高的組件
解決方法:在組態使用bindingRedirect讓實際執行是較
低的版本
  <runtime>
  <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly>
     <assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca"/>
  <bindingRedirect oldVersion="5.4.4.0" newVersion="5.4.0.0" />
     </dependentAssembly>
</assemblyBinding>
</runtime>

Case 2: AP參考版本較高、Lib參考版本較低
執行結果:在AP使用Lib的方法會出現找不到版本較低的組件
解決方法:在組態使用bindingRedirect讓實際執行是較的版本
<runtime>
  <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
  
<assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca"/>
  
<bindingRedirect oldVersion="5.4.0.0" newVersion="5.4.4.0" />
 
</dependentAssembly>
 
</assemblyBinding>
</runtime>

重點:
1.要以執行的AP的版本為主
2.此為懶人作法,版本或功能差異太大的話,還是更版最好。

參考文章
http://blog.darkthread.net/post-2011-03-31-assembly-binding-redirect.aspx

這個網誌中的熱門文章

IIS 設定只允許特定IP進入