Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

c# - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>  
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...