相同的DLL,两个不同的版本 [英] Same DLLs two different versions
问题描述
我有两个不同的项目,都使用NuGet的Newtonsoft.Json.
I have two different projects and both use Newtonsoft.Json from NuGet.
- 项目A使用版本9.0.1.
- 项目B使用版本11.0.1.
在构建项目时,一个dll会覆盖另一个dll,因为它们都被编译在同一个文件夹中.
When building the projects, one dll is overwriting the other dll, because they are both being compiled in the same folder.
如何将dll的编译重定向到单独的文件中,又如何说Project A使用9.0.1,Project B使用11.0.1?
How can I redirect the compilation for the dll's in seperate files and how can I say that Project A uses 9.0.1 and Project B uses 11.0.1?
最好有一个文件夹"Newtonsoft",并且有2个文件夹"11"和"9".在这些文件夹中是特定版本.(如果还有其他解决方案,那么我也可以与其他解决方案配合使用.)
It would be great to have a folder "Newtonsoft" and there are 2 folders "11" and "9". In those folders are the specific versions. (If there's another solution, then I'm also fine with the other one).
项目A和项目B都是插件",我的应用程序正在使用它们,其中包括来自Plugin-Folder的那些插件.这意味着我当前有一个使用以下dll的应用程序(它们是全部放在一个文件夹中):
Project A and Project B are both "Plugins", which are being used by an application by me, which includes those plugins from a Plugin-Folder.. This means I currently have an application which uses the following dll's (they are all in one folder):
- Project_A.dll
- Project_B.dll
- NewtonSoft.Json.dll(9.0.1或11.0.1)
ProjectA.dll
ProjectA.dll
这是我的app.config
This is my app.config
项目A:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="YamlDotNet" publicKeyToken="ec19458f3c15af5e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
项目B:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
<codeBase version="11.0.0.1" href="Newtonsoft.Json.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup></configuration>
推荐答案
已解决
通过执行以下操作使其正常工作:
Got it working by doing it like this:
在使用Newtonsfot.Json的任何方法/类之前,我使用以下两行:
Before I use any method/class of Newtonsfot.Json, I use the following two lines:
//The AssemblyResolve event is called when the common language runtime tries to bind to the assembly and fails.
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
如果应用程序抛出异常,将始终调用该事件
The event will always be called if the application would throw the exception
无法加载文件或程序集'Newtonsoft.Json,Version = 9.0.0.1,文化=中性,PublicKeyToken = 30ad4fe6b2a6aeed"或其中之一依赖关系.系统找不到指定的文件.
Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.1, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
该事件因此捕获了该事件并加载了正确的dll:
The event therefore catches the event and loads the correct dll:
Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly MyAssembly, objExecutingAssemblies;
string strTempAssmbPath = "";
objExecutingAssemblies = Assembly.GetExecutingAssembly();
AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
//Build the path of the assembly from where it has to be loaded.
//The following line is probably the only line of code in this method you may need to modify:
strTempAssmbPath = "C:\\Program Files\\PATH TO YOUR FOLDER" + "\\Newtonsoft.Json\\9.0.1";
if (!strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\";
//strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
strTempAssmbPath += strAssmbName.Name + ".dll";
break;
}
}
//Load the assembly from the specified path.
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
//Return the loaded assembly.
return MyAssembly;
}
信用: https://www.chilkatsoft.com/p/p_502.asp
这篇关于相同的DLL,两个不同的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!