views:

551

answers:

3

Hi, I have 4 assemblies:

  1. Tester.exe
  2. ToyInterface.dll
  3. ToyFactory.dll --> reference (ToyInterface.dll)
  4. Toy.dll --> reference (ToyInterface.dll)

Tester.exe

internal ICollection<string> Scan(string path){
  return ToyCollection = _reportFactoryType.GetMethod(FACTORY_GET_TOYS).
                            Invoke(_ToyFactory, null);
}

ToyFactory.dll

...try
{
// Load assembly:
 Assembly asm = Assembly.LoadFrom(fileFullPath);
 // Reflect ToyInterface types:
 IEnumerable<Type> types = asm.GetExportedTypes();
}

and I recieve an exception

Could not load file or assembly 'ToyInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"ToyInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

The binding info from fuslogvw.exe shows that the assembly is accessed in the Tester.exe Enviroment path. Why is that, and how can I change this?

A: 

Did you try to put all assemblies in same directory?

PerlDev
Thats the whole point. I dont want them in the same directory. Why should the tester and the toys HAVE to be together?
Feel
When you compile Toy.exe, if it, (or any dependant dll), has a reference to any dll, the Visual Studio compile should put that dll all into the output directory anyway... Unless one of the dlls is registered in the GAC... Is that the case?
Charles Bretana
If you don't want the dlls in the same output folder, then you will have to either 1) register them in the GAC. 2) load them dynamically, or 3) use complex CLR Loader version policy configuration files.
Charles Bretana
Charles, I AM loading them dynamically. thats the point. Tester.exe has no dependancy on either ToyFactory.dll or Toy.dll.They are NOT copied to the Tester.exe folder. That is why they are not found.
Feel
Please explain "use complex CLR Loader version policy configuration files. " ?
Feel
Charles Bretana
+1  A: 

You don't specify, but I assume Toy.exe is also dependant on ALL the other dlls? right? Cause if it's not, then the compile process will not copy them into Toy.exe's run executable directory. Check that durectory and make sure all three dlls are there, and are datetime stamped as of the latest compile time...

If all that's is kewl, then it's possible is that ToyInterface.dll has a dependency on some other dll (not mentioned) that is in your Visual Studio development folder but is not being copied to the runtime folder...

If you are dynamically loading these other dlls, don;t make the mistake of assuming that they load their own dependent dlls themselves. All Dependant Dlls are loaded based on the executable assemblies' Base Code folder (the folder it was laoded from), not the folders of the dlls that the Assembly.LoadFrom method may be executing in... So if you want a dll to be loaded from somewhere other than in Toy.exe's load folder (or s subdirectory of that folder), try specifying the Fully qualified Absolute path to the dll in the Assembly.LoadFrom() method, and see if that fixes things.

Charles Bretana
Toy.dll only knows ToyInterface.dllIt is dynamically loaded from ToyFactory.dllThat is also dynamically loaded from Tester.exeAnd somehow, Toy.dll is looking for its dependencies in Tester.exe's enviroment path.
Feel
You misunderstand, ALL assemblies are loaded into a single AppDomain, and a single process space, the AppDomain/process space initially created when toy.exe loads. So the folder that Toy.exe loads from is the defualt load folder. So toy.dll cannot load anything... Even though the line of code that loads it's dependecies is in toy.dll, it is ALWAYS toy.exes' AppDomain and Process that does the Loading. Since you are dynamically loading these other dlls, try explicitly specifying the full absolute path to the dll in the Assembly.LoadFrom() method and see if that fixes things.
Charles Bretana
A: 

I have found the problem! It appears I have been suffering from DLL binding hell. :(

This blog post helped me discover that I needed to use the Assembly.LoadFrom() function and not Assembly.LoadFile(). Now the Aseembly and its dependencies are being probed in the correct directories.

Thank you all for your help!

Feel