Skip to content

Instantly share code, notes, and snippets.

@jzwang-dev
Created February 22, 2021 14:27
Show Gist options
  • Save jzwang-dev/70af9a5a8a21878f5a203256d9d92658 to your computer and use it in GitHub Desktop.
Save jzwang-dev/70af9a5a8a21878f5a203256d9d92658 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace VersionConflicts
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, resolveArgs) =>
{
string assemblyInfo = resolveArgs.Name; // e.g "Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"
var parts = assemblyInfo.Split(',');
string name = parts[0];
var version = Version.Parse(parts[1].Split('=')[1]);
string fullName;
if (name == "Newtonsoft.Json" && version.Major == 11)
{
// 如果參考的組件為版本11,則載入V11資料夾下的組件
fullName = Path.Combine(HttpRuntime.AppDomainAppPath, @"bin\V11\Newtonsoft.Json.dll");
}
else if (name == "Newtonsoft.Json" && version.Major == 12)
{
// 如果參考的組件為版本12,則載入V12資料夾下的組件
fullName = Path.Combine(HttpRuntime.AppDomainAppPath, @"bin\V12\Newtonsoft.Json.dll");
}
else
{
return null;
}
return Assembly.LoadFile(fullName);
};
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment