Multi Version DLL Use on the same C# application

107 views Asked by At

I have got a web application with multiple projects that is using newtonsoft 6.0.0.0. Now I have added a stripe dll to two of the projects which turns out that it needs higher newtonsoft. What I need to is that instead of upgrading all newtonsoft library across the web application projects, and also in other apps as there are some shared library, I want to limit the use of newtonsoft higher version to only stripe so that all projects still use the lower version even the projects using stripe can still use the lower version for non stripe logics.

What I tried so far is I added below to the project files:

      <ItemGroup>
        <!-- Place the new snippet here -->
        <Content Include="..\..\Comps\Json.NET\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>Stripe\Newtonsoft.Json.dll</Link>
        </Content>
        <Content Include="..\..\Comps\Json.NET\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>Newtonsoft.Json.dll</Link>
        </Content>
        
      </ItemGroup>

Also I added the lower version as a reference

<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <HintPath>..\..\Comps\Json.NET\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>    

I have also added this in Web.config

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="6.0.0.0" />
    <codeBase> version="6.0.0.0" href="Newtonsoft.Json.dll"</codeBase>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="9.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
    <codeBase> version="13.0.0.0" href="Stripe\Newtonsoft.Json.dll"</codeBase>
  </dependentAssembly>

But I am getting error loading the web app.

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

Any help to resolve this will be appreciated.

2

There are 2 answers

0
Palle Due On BEST ANSWER

Your xml is wrong. The attributes have been made content. It should look like this:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="6.0.0.0" />
    <codeBase version="6.0.0.0" href="Newtonsoft.Json.dll"></codeBase>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
    <bindingRedirect oldVersion="9.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
    <codeBase version="13.0.0.0" href="Stripe\Newtonsoft.Json.dll"></codeBase>
  </dependentAssembly>
3
Palle Due On

That will not work. It only takes the first redirect rule with a given assemblyIdentity. You can see that here

In case of a conflict in redirection, the first matching redirection statement in the configuration file is used.