Loading multiple versions of the same assembly
Asked Answered
A

3

20

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?

Aubervilliers answered 15/12, 2010 at 14:55 Comment(0)
D
16

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

Dillie answered 15/12, 2010 at 16:18 Comment(2)
How could I tell it to load either the new version or the old version?Interchange
@Interchange i donot understand your question. What exactly do you want to achive? newVersion=1.0.0.0 tells to use the old version 1.0.0.0.Dillie
E
6

Here are a couple posts from here on SO that describe how to load multiple versions of the same assembly:

This post describes how to reference two different versions of log4net. See @Joe B.'s comment under the accepted answer for a bit more detail on exactly how he solved his problem.

3rd party libraries refer to different versions of log4net.dll

That answer refers to this link:

Using different versions of the same assembly in the same folder

Within this thread, there is a caution about loading different versions of the same assembly in the same context and references this link on MSDN:

http://msdn.microsoft.com/en-us/library/dd153782.aspx#avoid_loading_multiple_versions

Here is another with an answer that suggests using AssemblyResolve: Reference two equal assemblies, only public keys differ

Ellipticity answered 15/12, 2010 at 15:43 Comment(0)
J
1

If you have two different versions of the same component, than this means that both versions can differ not only in implementation but also in the number of operations they expose? If this is the case, how can you switch versions if some functionality is only in one of two versions?

Anyway, if you want to load the two versions you would have to do it in two separate application domains, because then you can unload the one you don't need afterwards. Then you could execute your logic in the correct application domain, as described in How do I create an application domain and run my application in it?.

Jepson answered 15/12, 2010 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.