tags:

views:

57

answers:

4

I have a dll named A.dll with strong name, another assembly B references A.dll. I place A.dll in c:\myapp, also A.dll is installed in GAC. I hope assembly B loads the A.dll from c:\myapp not GAC. But it always load the A.dll from GAC. My question is how to load a referenced dll from specified local location not GAC. Thanks.

+2  A: 

According to the MSDN page about how the CLR loads assemblies, the CLR will load assemblies from the GAC before attempting to load assemblies from the working directory. There doesn't appear to be a way around this.

Alternatively, you could just not add the assembly to the GAC so that the CLR would be forced to find the assembly by probing.

CodeSavvyGeek
Hi, Thanks for reply. I tried configuration file but it still search from GAC first. The search sequence of loading a strong name dll is:1. GAC2. The root folder of the app3. Configuration fileI readed the article you provided.1.Determines the correct assembly version (my case: the version is same as that in GAC)2.Checks whether the assembly name has been bound to before (my case: No, or how to do?)3.Checks the global assembly cache (My case: find here, load from GAC)4.Probes for the assembly (based on the Configuration file)If I misunderstood, please correct me.
I did add configuration file for my App, and the assembly must be added to GAC for some reason.
@jack2000: You're right, configuration files don't appear to help the situation. I removed that section of my answer.
CodeSavvyGeek
+2  A: 

You'll have to give it a different [AssemblyVersion]. Which ultimately makes sense, if the version numbers are the same then there's no reason that the GAC version wouldn't be good.

Hans Passant
I was assuming the assemblies were actually the same in this scenario. If they aren't exactly the same, they should definitely have different identities so the CLR can differentiate them.
CodeSavvyGeek
Keep it in GAC, because other Apps are using it. Load local A.dll for my App, because I do some change in A.dll, but its version is unchanged, I need the new A.dll for my App.Yes, the trouble is they have the same identity.
If you are doing some change in A.dll, why does the identities remain the same? Just changing the name of the assembly will help solve this situation.
earlNameless
A: 

The only way is by using Assembly.LoadFile() to load your assembly. This allows you to specify a path to load.

There are several gotchas that make this very painful:

  • You have to do Assembly.LoadFile() before the assembly is loaded automatically
  • None of the dependencies of A.dll are loaded automatically, you will have to load all of them by hand
  • A.dll would not be loaded into LoadFrom context (see msdn for more information), which effectively means any code that references A.dll cannot just use the types from A.dll, you must use reflection
earlNameless
This does not work. Even if you specify the path of the assembly, the CLR will check the GAC and load an equivalent version from there if it exists.
CodeSavvyGeek
A: 

It is possible to do this by hosting the CLR and implementing IHostAssemblyManager and IHostAssemblyStore. This is like using a bazooka to kill a mosquito though.

Logan Capaldo