0

This is strange...

In a project of mine I need to load external dll's at runtime. I've done this frequently before and I thought I had stepped on all the mines there where but this one has got the best of me so far.

It's very very basic really. I use Assembly.LoadFrom("c:\\test\\mytestlibrary.dll") but Fusion cannot find the file (I get a FileNotFoundException).

I have examined the fusion logs as usual and it just doesn't make sense. Are there some circumstances that would somehow prevent Fusion from finding a file even when I provide the complete and absolute path to it? I suspected the dll in question needed some other assembly but looking at the fusion logs doesn't indicate this. Besides, the test library references nothing that's not also referenced by the host assembly.

Any suggestions?

Jonas Rembratt
  • 1,550
  • 3
  • 17
  • 39
  • Does the test assembly reference different versions of anything vs the main assembly? – Chris Shain Apr 11 '12 at 17:13
  • Does the target platform differ (32bit vs 64bit)? – Sascha Apr 11 '12 at 17:14
  • 1
    And probably a stupid question, but are you absolutely 100% sure that the file `C:\test\mytestlibrary.dll` exists? – Chris Shain Apr 11 '12 at 17:17
  • 2
    "Strange" problems like this require *lots* of details in your question so we don't have to ask a bunch of follow-up questions. – Cody Gray - on strike Apr 11 '12 at 17:19
  • Have you actually verified that file is there by exact path you are trying to load it from? – Alexei Levenkov Apr 11 '12 at 17:19
  • @CodyGray, I completely agree with you that the OP should have provided more details but I strongly suspect that if he has asked himself all those questions he would probably already have found the problem and wouldn't have posted the question on SO :-) – Darin Dimitrov Apr 11 '12 at 17:20
  • Well, everyone. The problem is out of the ordinary but I did fail to mention one important fact: My code runs inside another host process which I have not compiled and everything seems to stem from that fact. As I moved on I managed to isolate the issue and finally got it to work. Then, by re-injecting code that seemingly has nothing to do with Fusion, I get the problem back. Well, I'm afraid this is just to special a case for anyone to help out. Thanks anyway and sorry for not being able to provide more details. – Jonas Rembratt Apr 11 '12 at 17:52

1 Answers1

1

Perhaps this blog entry by Suzzanne Cook will provide some clues?

For FileNotFoundException: At the bottom of the log will be the paths that Fusion tried probing for this assembly. If this was a load by path (as in Assembly.LoadFrom()), there will be just one path, and your assembly will need to be there to be found. Otherwise, your assembly will need to be on one of the probing paths listed or in the GAC if it's to be found.

You may also get this exception if an unmanaged dependency or internal module of the assembly failed to load. Try running depends.exe on the file to verify that unmanaged dependencies can be loaded. Note that if you re using ASP.NET, the PATH environment variable it's using may differ from the one the command line uses. If all of them could be loaded, try ildasm.exe on the file, double-click on "MANIFEST" and look for ".file" entries. Each of those files will need to be in the same directory as the manifest-containing file. -- http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx

IIRC, the fusion log should also show the list of paths sought. Is your path included? Further, is the assembly already loaded as a project reference or otherwise previously in the same app domain?

Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • Yes, I have examined the Fusion log but the thing is: When using Assembly.LoadFrom/LoadFile you specify the exact path to the assembly so there shoudl be no need for Fusion to start probing. In fact, using Assembly.LoadFrom/LoadFile is the common way to resolve probing issues when using the AppDomain approach to loading assemblies dynamically. Thanks anyway. – Jonas Rembratt Apr 11 '12 at 17:57
  • Well she wrote the thing. You say you examined the log - is it probing (and listing) other probe paths or not? – Jason Kleban Apr 11 '12 at 18:04
  • No, its not logging any paths, and that's what's so strange. Fusion starts probing examining the (host) app.config, machine.config file and so on, just like if I had initiated the load from a call to AppDomain.Load() rather than with Assembly.LoadFile(). – Jonas Rembratt Apr 11 '12 at 18:27
  • You wrote just there "LoadFile" but we've been talking about "LoadFrom" so I suspect this was a slip. Have you tried [Assembly.LoadFile](http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx) which would bypass Fusion? – Jason Kleban Apr 11 '12 at 18:32