Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference different version of the same assembly into a single project?

In my solution, I have several projects which use Log4Net (1.2 and 2.5).

Then I have a project where I do all the unit testing (of the other projects). So I'm in a situation where depending on what I test/mock I would need Log4Net 1.2 or 2.5.

I read you could support different version of a single assembly in an application (using codebase etc.) but is it even possible to support different version of a single assembly into a project? If so, how?

EDIt:

Here's a tiny (2 classes, 2 methods, 2 constructors) project showing my issue:

https://srv-file1.gofile.io/download/EQFdOs/212.76.254.142/Log4NetMulti.zip

(I hope the link work)

like image 749
Serge Intern Avatar asked May 19 '16 06:05

Serge Intern


2 Answers

Thanks to Pikoh's advices I managed to make it work. But since it requires more than just using alias and rename dll, I'll write down the whole process.

  1. Remove the existing reference (ex: log4net.dll) from the project
  2. Add a folder to the project to store the several dll (ex: /Libs)
  3. Add the several dll in it and give them unique file names (like log4net.1.2.10.0.dll, log4net.1.2.15.0.dll)
  4. Go to property on those files and chose "Copy: Always" or "Copy: Copy if newer"
  5. Add references to those files
  6. Give those references unique aliases (ex: log4net_1_2_10_0, log4net_1_2_15_0)
  7. In you code, make use of the aliases, using "extern alias" keywords.

    extern alias log4net_1_2_10_0;
    
    using log4net_1_2_10_0.log4net;
    using System.Web;
    ...
    
  8. Add codebases into your config file. Those should refer to the dll you placed into your folder, with the proper version and token.

    <runtime> 
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
          <codeBase version="1.2.15.0" href="Libs/log4net.1.2.15.0.dll"/>
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
          <codeBase version="1.2.10.0" href="Libs/log4net.1.2.10.0.dll"/>
        </dependentAssembly>
      </assemblyBinding>
    </runtime>  
    
like image 193
Serge Intern Avatar answered Oct 18 '22 02:10

Serge Intern


You can reference diferent versions of the same dll using Alias, as is explained here MSDN:

Add the reference to both the dlls in your client application solution. Then in the Solution Explorer under the reference node select the first (old version) class library. In the property window change Aliases field from global to oldVer. Make similar change after selecting the newer class library as well. You are then good to go…

like image 5
5 revs, 4 users 77%SuperG Avatar answered Oct 18 '22 04:10

5 revs, 4 users 77%SuperG