我有两个不同的项目,都使用NuGet的Newtonsoft.Json。
在构建项目时,一个dll会覆盖另一个dll,因为它们都被编译在同一个文件夹中。
如何将dll的编译重定向到单独的文件中,又如何说Project A使用9.0.1,Project B使用11.0.1?
最好有一个文件夹“ Newtonsoft”,并且有2个文件夹“ 11”和“ 9”。在这些文件夹中是特定版本。 (如果还有其他解决方案,那么我也可以与其他解决方案配合使用。)
项目A和项目B都是“插件”,我的应用程序正在使用它们,其中包括来自Plug-Folder的那些插件。这意味着我当前有一个使用以下dll的应用程序(它们是全部放在一个文件夹中):
ProjectA.dll
这是我的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>
答案 0 :(得分:0)
您可以将程序集放置在其他文件夹中(不一定直接在bin文件夹中),并使用codeBase元素:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Some.API" publicKeyToken="12ab3cd4e5f6abcd"
culture="neutral" />
<codeBase version="1.0.0.0" href="v1\Some.API.dll" />
<codeBase version="2.0.0.0" href="v2\Some.API.dll" />
<!-- INFO: The version attribute represents an assembly
version which doesn't always have to match the
NuGet package version.
The codebase attribute can be anywhere on
the local intranet or the Internet. -->
</dependentAssembly>
</assemblyBinding>
</runtime>
答案 1 :(得分:0)
已解决
通过执行以下操作使其正常工作:
在使用Newtonsfot.Json的任何方法/类之前,我使用以下两行:
//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);
如果应用程序抛出异常,将始终调用该事件
无法加载文件或程序集'Newtonsoft.Json,Version = 9.0.0.1, 文化=中性,PublicKeyToken = 30ad4fe6b2a6aeed”或其中之一 依赖性。系统找不到指定的文件。
事件因此捕获了事件并加载了正确的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;
}