Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why FileNotFoundException when I Assembly.Load an assembly that is surely present?

In my Windows Azure role C# code I do the following:

Assembly.Load( "Microsoft.WindowsAzure.ServiceRuntime" );

and FileNotFoundException is thrown. The problem is an assembly with such name is present and has even loaded before the code above is run - I see a corresponding line in the debugger "Output" window and when I do:

AppDomain.CurrentDomain.GetAssemblies().Any(
    assembly => assembly.FullName.StartsWith("Microsoft.WindowsAzure.ServiceRuntime"));

the result is true and if I use Where(), then SingleOrDefault() I get a reference to a corresponding Assembly object.

Why can't I load an assembly with Assembly.Load()?

like image 792
sharptooth Avatar asked Jun 22 '11 12:06

sharptooth


2 Answers

That Load() call can only succeed if Microsoft.WindowsAzure.ServiceRuntime.dll is stored in your app's probing path. By default the same directory as your EXE. Problem is, it isn't stored there, it is stored in the GAC.

The point of the GAC is to act as a depository of assemblies with the same name but different [AssemblyVersion]s, culture or processor architecture. Which is the problem with your Load(), you don't specify any. There is no reasonable way that fusion can pick an assembly for you, it is apt to give you the wrong one. So it doesn't bother, even if there is only one to pick from.

Specifying the full AsssemblyName.FullName is required. Use Project + Add Reference to avoid.

like image 139
Hans Passant Avatar answered Sep 21 '22 21:09

Hans Passant


You should load it with a full assembly qualified name.

like image 27
Matías Fidemraizer Avatar answered Sep 19 '22 21:09

Matías Fidemraizer