Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple versions of the same assembly

I'm working with a third-party assembly and unfortunately I now need to load their latest and a previous version into my project so at runtime I can decide which one to load. I only ever need one, not both.

With this in mind, I am also dependent upon the types provided by the components so I cannot load from reflection and query every time for the method/events/interfaces that I want to use. I have seen some mention of handling this via AppDomains but am not sure how to proceed.

Would the process be to code against one version of the component and then at run-time (using the AppDomain) swap in the correct DLL I want to be consumed? So I would only be handling this at startup?

like image 825
GT. Avatar asked Dec 15 '10 14:12

GT.


1 Answers

If both assemblies are compatible you can define in the app.exe.config or web.config file to always use the new version by declaring bindingRedirect .

example

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
        <dependentAssembly>
            <assemblyIdentity name="Regcode" publicKeyToken="b03f5f7f11d50a3a" culture=""/>
            <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/>
        </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

this config entry for dotnet 1.0 tells the asembly loader always to use version 1.0.3300.0 no matter what is compiled into the calling exe. The same is possible with newer dotnet versions

like image 54
k3b Avatar answered Nov 09 '22 23:11

k3b