2

I am writing a test app to perform some regression tests. The idea is to be able to run test over multiple versions of a library. My goal is to load the dlls up in a Dictionary where the key is the version string (such as "3.0.0.0") and the value is the Assembly instance. I am able to dynamically load one assembly and call a method on it, but when I try to load a second one, I get the following exception:

The located assembly's manifest definition does not match the assembly reference.

I am loading the assemblies with the following line:

asm = Assembly.LoadFrom(lib, hash, System.Configuration.Assemblies.AssemblyHashAlgorithm.MD5);

'lib' is the complete filename and path of the dll. 'hash' is the md5 sum of the dll.

I looks like even though I am telling Windows "use this dll", it looks at the name and says "I already have that one" and uses the previously loaded one and since the hash doesn't match, it fails. Originally, the dlls being loaded did not have an Assembly Version set, so I set it on 4 different versions, but it still threw the same exception.

What is the fix for this?

Jordon

2 Answers2

5

You cannot load more than one version of the same assembly into single AppDomain. Also once loaded, assembly cannot be unloaded from AppDomain (with exception of dynamically created transient assemblies in .NET 4), but it is possible to unload whole AppDomain (which unloads all assemblies, that were loaded in it). Therefore you must load each version of your assembly into separate (newly created) AppDomain. Also be very careful to NOT pass any reference to loaded assembly between individual AppDomains (and especially to main AppDomain, where your testing app resides), because otherwise .NET will try to load assembly into every AppDomain, where reference to this assembly appears and you will end up with the same error again.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • Where did you get this information? Using the code in [this answer](http://stackoverflow.com/a/2023445/389899) I was able to load different versions of the same assembly into the same AppDomain. Am I missing something? – blachniet Jul 10 '13 at 21:06
  • I too am curious about the deviation in methods-to-success for this problem. surely folks have used the code @blachniet refers to, yet countless others indicate otherwise. What's the difference? It seems you can load different versions of the same assembly and then create identically-named types from each that will execute different code. Is it safe to say this would break if you didn't create the type via the assembly, but instead just the typename via Activator? Or is the key difference what you want to do ***after***, for instance, unloading the dlls? – JoeBrockhaus Nov 26 '14 at 22:47
  • Basically, I need to know if I should create one AppDomain which can process multiple plugin versions, then be unloaded, or if I have to create an AppDomain for each plugin version. – JoeBrockhaus Nov 26 '14 at 22:48
  • @JoeBrockhaus Did you ever figure this out? I have a similar problem where I want to load multiple version of a plug-in simultaneously. – Eric Anastas Apr 21 '17 at 11:21
1

You'll need to the assemblies into separate AppDomains.

Ryan Rinaldi
  • 4,119
  • 2
  • 22
  • 22