How to load same assembly of different version in one application?

3

I have Windows forms application in C#. I am in need to make use of the below dll.

Interop.SHDocVw.dll

The problem is that I need to refer two different versions of this same assembly.

I tried keeping the dll in two different locations & tried to add but not allowing.

Following the below link, I tried to load two version of a assembly on runtime, but somehow not sure how do I do that.

https://www.codeproject.com/Tips/373589/Load-same-assemblies-with-different-versions

This is how my app.config looks like:-

<runtime>
<assemblyBinding  xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Interop.SHDocVw.dll" publicKeyToken="db7cfd3acb5ad44e" />
    <codeBase version="1.1.0" href="...\watin-core\Interop.SHDocVw.dll"/>
  </dependentAssembly>

  <dependentAssembly>
    <assemblyIdentity name="Interop.SHDocVw.dll" publicKeyToken="632609b4d040f6b4" />
    <codeBase version="1.3.0" href="...\winforms\Interop.SHDocVw.dll"/>
  </dependentAssembly>

Currently the exception it is throwing:-

System.IO.FileLoadException: Could not load file or assembly 'Interop.SHDocVw, Version=1.1.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)
 File name: 'Interop.SHDocVw, Version=1.1.0.0, Culture=neutral, 
 PublicKeyToken=null'
 at myApp.AppUI.InitializeComponent()

How do I address this?

c#
.net
.net-assembly
asked on Stack Overflow Feb 5, 2018 by Kgn-web • edited Dec 21, 2018 by halfer

2 Answers

3

as in http://geekswithblogs.net/narent/archive/2008/11/11/126940.aspx :

1) Edit your csproj file (right click on project in solution explorer , then unload project then, modify csproj file)

2) in this csproj file, add in ItemGroup containing Reference tags: (here an example with Office Interop 15 and 16)

<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
  <HintPath>C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll</HintPath>
  <Aliases>Test2</Aliases>
  <Private>False</Private>
</Reference>

and

<Reference Include="Microsoft.Office.Interop.Excel, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
  <HintPath>C:\Program Files (x86)\Microsoft Office\root\Office16\DCF\Microsoft.Office.Interop.Excel.dll</HintPath>
  <Aliases>Test1</Aliases>
  <Private>False</Private>
</Reference>

as you can see, I've added Aliases : Test1 and Test2

3)Then, reload project and in your .NET Code , add at the very first line of your code (before any "using"):

extern alias Test1;
extern alias Test2;

then in your code you can use the same assembly with 2 distinct versions:

Test1.Microsoft.Office.Interop.Excel.Application app1;
Test2.Microsoft.Office.Interop.Excel.Application app2;

screenshot of the c# code window

.csproj file

Update 2019 dec 18 after comment from user203687 (I have downloaded your sample code and made it work with the app.config add-on below) : (adding a codebase reference solved the issue)

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="SampleLib" publicKeyToken="2901d9e7607fc6eb" />
    <codeBase version="1.0.0.0" href="YourFullPathTo\Lib-v1\SampleLib.dll" />
    <codeBase version="1.5.0.0" href="YourFullPathTo\Lib-v2\SampleLib.dll" />
  </dependentAssembly>
 </assemblyBinding>
 </runtime>
answered on Stack Overflow Feb 5, 2018 by Xavave • edited Dec 18, 2019 by Xavave
-1

I think when you say - "you can run two different versions of same assembly side by side" means that you can have two versions of assembly allowed to be loaded into same app domain.

This means in your application :

Assembly1 (lets say you name your cs proj as Assembly1) can refer version 1.1 of Interop.SHDocVw.dll

And Assembly2 ((lets say you name your cs proj as Assembly2) can refer version 1.3 of Interop.SHDocVw.dll.

Your final o/p directory will have Assembly1.dll, Assembly2.dll and both the versions of Interop.SHDocVw.dll.

answered on Stack Overflow Feb 5, 2018 by rahulaga_dev

User contributions licensed under CC BY-SA 3.0