在作asp.net MVC 開發時,由於引用的dll 中使用了更高版本的 Newtonsoft.Json ,致使運行時發生錯誤,web
查資料說是由於webApi使用了Newtonsoft.Json 致使了,個人項目中沒有用到webapi,所以,在Global.asax 中把 下面這行代碼屏蔽後,果真再也不報錯了。api
// WebApiConfig.Register(GlobalConfiguration.Configuration);
可是,當我在發佈該項目時,又遇到了如下錯誤:app
沒法解決「Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed」與「Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed」之間的衝突。正在隨意選擇「Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed」。 1> 請考慮使用 app.config 將程序集「Newtonsoft.Json, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed」從版本「4.5.0.0」[D:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll]從新映射到版本「7.0.0.0」[E:\project\.....\Debug\Newtonsoft.Json.dll],以解決衝突並消除警告。 1>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3247: 發現同一依賴程序集的不一樣版本間存在衝突。在 Visual Studio 中,請雙擊此警告(或選擇此警告並按 Enter)以修復衝突;不然,請將如下綁定重定向添加到應用程序配置文件中的「runtime」節點: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" /></dependentAssembly></assemblyBinding>
雖然該錯誤並無影響到程序運行,可是我仍是以爲不爽,細看說明,原理微軟在提示咱們時,就把解決方法告訴了咱們:asp.net
不然,請將如下綁定重定向添加到應用程序配置文件中的「runtime」節點: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" /></dependentAssembly> </assemblyBinding>
意思是,把上面這串xml 代碼放到runtime 節點裏,把用到0.0.0.0-7.0.0.0 版本的dll 的地方,所有從新定位到7.0.0.0 版本,由於這個dll基本能夠兼容,因此問題獲得解決。ui
推而廣之,其餘的dll衝突也能夠用這種方式解決。spa