今天研究了一下.net的web api,寫了幾個測試方法,運行報錯,上網查找問題后發現,Newtonsoft.Json作者答復4.5版本的dll無法完成web api里ef entity序列化工作,於是升級json庫到6.0后,本地調試通過,再將程序發布到測試機上,報了久違了的黃白頁(自從改造成angularjs后基本沒有黃白頁了)
按理說已經將項目引用的DLL升級到6.0了,怎么還會報找不到4.5的錯呢?經過同事提醒,可能是別的DLL還在依賴4.5,他還建議用Reflector查看引用來確認,果不其然
主程序居然引用了同名的兩不同版本的DLL,但是本機能運行,為什么丟到測試機上就報錯,原來在我升級dll時系統自動在web.config中加入了一下節點
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
用這個方法來兼容老版本的Json,這個方法適合於新版本dll兼容老版本dll的情況,如果碰到了一個項目中確實要同時引用兩個不同版本的DLL時,譬如要用到老版本DLL中的數據導出方法,又要同時用到新版本DLL中的數據導入方法時,就需要修改成以下節點形式
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Castle.DynamicProxy2" publicKeyToken="407dd0808d44fbdc" /> <codeBase version="2.1.0.0" href="v2.1\Castle.DynamicProxy2.dll" /> <codeBase version="2.2.0.0" href="v2.2\Castle.DynamicProxy2.dll" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" /> <codeBase version="1.1.0.0" href="v2.1\Castle.Core.dll" /> <codeBase version="1.2.0.0" href="v2.2\Castle.Core.dll" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>