Search
Close this search box.

How to use multiple versions of assembly

1. Create 2 shared assemblies with different versions (1.0.0.0 and 2.0.0.0).

2. For assembly 1 include the following reference in project file.

   <Reference Include="TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=508b5c5f7455410a, processorArchitecture=MSIL">
      <HintPath>..\..\TestAssembly\T1\TestAssembly.dll</HintPath>
      <Aliases>Test1</Aliases>
      <Private>False</Private>
    </Reference>

Hint Path Vs Reference Path
As for the "Referece Path" or "Hint Path" in the VS.NET project's setting
files, they're all design-time /dev-time setting used by the IDE. As
described in the IDE's help doc, "Reference Path" are used by the VS.NET
IDE to load all the assembliy references when the Project is loaded into
the VS.NET IDE. And the "Hit Path" is mainly used for building time, when
the IDE will build the project, it'll locate the assemblies which is
required to link through the "Hint Path". Anyway, they're all internally
used by the VS.NET IDE, and is possibly to change in sequential version and
they have nothing to do with the .NET framework CLR's runtime assembly
locating.

For .net framework CLR's runtime assembly locating, it'll follow a well
defined steps, generally, it'll check GAC (if strong-named) first, then,
codebase settting , and private path probing. Here is a MSDN reference
which describing the .NET framework's runtime assembly locating.

3. Form assembly 2 include the following reference in the project file.

  <Reference Include="TestAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=275b13b4e9b16f25, processorArchitecture=MSIL">
      <HintPath>..\..\TestAssembly\T2\TestAssembly.dll</HintPath>
      <Aliases>Test2</Aliases>
      <Private>False</Private>
    </Reference>

4. While refering the above assemblies set extern aliases for both the assemblies

extern alias Test1;
extern alias Test2;

5. And then we can use Test1 and Test2 as 2 different namespaces

Test1.TestAssembly.TestClass t1 = new Test1.TestAssembly.TestClass();
MessageBox.Show(t1.Add(2, 3).ToString());

Test2.TestAssembly.TestClass t2 = new Test2.TestAssembly.TestClass();
MessageBox.Show(t2.Add(2, 3, 4).ToString());

Please note the project referencing both the assemblies will be compiled with the following warnings.

Warning 1 
No way to resolve conflict between “TestAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=275b13b4e9b16f25” and “TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=508b5c5f7455410a”. Choosing “TestAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=275b13b4e9b16f25” arbitrarily. WindowsApplication1

Warning 2 
The referenced component ‘TestAssembly’ could not be found.

Above scenario is about when we refer both the assemblies of (same name different versions) in the same code file ..but what if we want to override the functionality of an assembly ver 1.0 with ver 2.0.

put following <runtime> tag under configuration settings in the aplication’s config file.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="<assemblyName>" publicKeyToken="<publicKeyToken>" culture="<culture>" />
<!--<assemblyIdentity name="TestAssembly" publicKeyToken="5ce1b9763dbb3e84" culture="neutral" />->
<bindingRedirect oldVersion="<oldVersion>" newVersion="<newVersion>" />
<!--<bindingRedirect oldVersion="1.0.0.0" newVersion="1.1.0.0" />-->
</dependentAssembly>
</assemblyBinding>
</runtime>

posted on Tuesday, November 11, 2008 9:38 AM

This article is part of the GWB Archives. Original Author: Narendra Tiwari

Related Posts