admin管理员组

文章数量:1355710

I've encountered a problem regarding referencing multiple versions of a DLL. I've tried various methods to solve it. I'm seeking help.Thanks!

In a program, I referenced the DLL component of version 13.0.0.0, and everything was normal. Then I referenced a third - party component DLL, which might have used a different version of the DLL (Newtonsoft). As a result, the following error occurred.

System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I've searched for and attempted some solutions available online to address the multi - version issue, but none of them have been successful.

1、config change

<runtime>  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
    <dependentAssembly>  
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>  
      <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>  
    </dependentAssembly>  
  </assemblyBinding>  
</runtime>  

OR

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect version="4.0.0.0" href="D:\WebService\TaskWebService\bin\Net40\Newtonsoft.Json40.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect version="13.0.0.0" href="D:\WebService\TaskWebService\bin\Newtonsoft.Json.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

2、alias reference

extern alias NewJson40;
using NewJson40NameSpace = NewJson40::Newtonsoft.Json;
using NewJson40LinqNameSpace = NewJson40::Newtonsoft.Json.Linq;
......

I've encountered a problem regarding referencing multiple versions of a DLL. I've tried various methods to solve it. I'm seeking help.Thanks!

In a program, I referenced the DLL component of version 13.0.0.0, and everything was normal. Then I referenced a third - party component DLL, which might have used a different version of the DLL (Newtonsoft). As a result, the following error occurred.

System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I've searched for and attempted some solutions available online to address the multi - version issue, but none of them have been successful.

1、config change

<runtime>  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
    <dependentAssembly>  
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>  
      <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>  
    </dependentAssembly>  
  </assemblyBinding>  
</runtime>  

OR

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect version="4.0.0.0" href="D:\WebService\TaskWebService\bin\Net40\Newtonsoft.Json40.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect version="13.0.0.0" href="D:\WebService\TaskWebService\bin\Newtonsoft.Json.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

2、alias reference

extern alias NewJson40;
using NewJson40NameSpace = NewJson40::Newtonsoft.Json;
using NewJson40LinqNameSpace = NewJson40::Newtonsoft.Json.Linq;
......
Share Improve this question edited Mar 31 at 3:18 Brando Zhang 28.7k6 gold badges42 silver badges70 bronze badges asked Mar 31 at 1:48 KarosKaros 11 bronze badge New contributor Karos is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • nickcraver/blog/2020/02/11/binding-redirects – stuartd Commented Mar 31 at 2:09
  • What version of .NET are you running? – mjwills Commented Mar 31 at 5:15
  • You say you are referencing version 13 of Json.Net. And when you have checked that that that is the version that actually makes it into your output folder of your solution/project when building why do you try to do a bindingredirect for up to version 6.0.0.0? – Ralf Commented Mar 31 at 14:52
Add a comment  | 

1 Answer 1

Reset to default 0

Binding redirect is not working for 6 or later, you could refer this document for more details reference.

This article is specific to .NET Framework. It doesn't apply to newer implementations of .NET, including .NET 6 and later versions.

The workaround for this is using the System.Text.Json instead of the Newtonsoft.Json. Or you could dynamically load the assembly when you want to use it.

本文标签: cI39ve encountered a problem regarding referencing multiple versions of a DLLStack Overflow