6

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()?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 3
    Maybe it is not this assembly, but one of its dependencies, such as a native dll, that is missing? – Armen Tsirunyan Jun 22 '11 at 12:53
  • @Armen Tsirunyan: Maybe, but the very same assembly has already been loaded - I see that in debugger output. – sharptooth Jun 22 '11 at 13:07
  • I turn to use absolute path: `Assembly.LoadFrom(path)`, I'm on web server and I use the path as `string path = HttpRuntime.AppDomainAppPath + "bin\\XXXX.dll"` – yu yang Jian May 30 '18 at 07:01

4 Answers4

4

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

You should load it with a full assembly qualified name.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I don't want to mess with the version info - I don't care of it. How do I load the assembly then? – sharptooth Jun 22 '11 at 13:06
  • Sadly, if it's a signed assembly, you must load it with this full assembly qualified name. No way. Imagine if you install it in the GAC and you've 2 o 3 versions of same assembly? What would be the one that you get when you load without a qualified name? I know you'd prefer to avoid version info, but...? :D – Matías Fidemraizer Jun 22 '11 at 13:42
  • Unless you use Assembly.LoadFrom - but then you have to provide the file path. – ozczecho Jun 22 '11 at 13:54
  • Yeah, @ozczecho, you're right. But I believe his case has no other approach, he's loading a Microsoft assembly that would be installed in different locations depending on user's machine, so I wouldn't suggest to use LoadFrom in this case. – Matías Fidemraizer Jun 22 '11 at 13:59
3

From the MSDN documentation:

// You must supply a valid fully qualified assembly name.            
        Assembly SampleAssembly = Assembly.Load
            ("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");
ozczecho
  • 8,649
  • 8
  • 36
  • 42
2

The documentation for Assembly.Load says that you're meant to supply the full name for the assembly (including e.g. version information).

Using just a plain name for the assembly will fail if the assembly is normally loaded from e.g. the GAC. E.g.:

        try
        {
            Assembly.Load("System");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        Console.WriteLine(AppDomain.CurrentDomain.GetAssemblies().Any(
        assembly => assembly.FullName.StartsWith("System")));
        Console.ReadLine();

Exhibits similar behaviour.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448