开发者

Assembly references for dymanically loaded DLLs

I have 4 assemblies:

  1. Tester.exe
  2. ToyInterface.dll
  3. ToyFactory.dll --> reference (ToyInterface.dll)
  4. Toy.dll --> refer开发者_开发知识库ence (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?


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!


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.


Did you try to put all assemblies in same directory?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜