using System; using System.Reflection; using System.Web.Http; namespace GXWebApi { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { // 处理 Kingdee.BOS.WebApi.Client 引用 Newtonsoft.Json 4.0.0.0 (PublicKeyToken=null) 的问题 // 将其重定向到当前加载的 Newtonsoft.Json 13.0 版本 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; GlobalConfiguration.Configure(WebApiConfig.Register); } private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var assemblyName = new AssemblyName(args.Name); // Kingdee.BOS.WebApi.Client 引用的是 Newtonsoft.Json 4.0.0.0, PublicKeyToken=null // 将其重定向到已加载的 Newtonsoft.Json 版本 if (assemblyName.Name.Equals("Newtonsoft.Json", StringComparison.OrdinalIgnoreCase)) { // 返回当前 AppDomain 中已加载的 Newtonsoft.Json 程序集 foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (assembly.GetName().Name.Equals("Newtonsoft.Json", StringComparison.OrdinalIgnoreCase)) { return assembly; } } // 如果尚未加载,尝试从文件加载 try { var baseDir = AppDomain.CurrentDomain.BaseDirectory; var dllPath = System.IO.Path.Combine(baseDir, "bin", "Newtonsoft.Json.dll"); if (System.IO.File.Exists(dllPath)) { return Assembly.LoadFrom(dllPath); } } catch { // ignore } } return null; } } }