为什么FileNotFoundException异常时,我的Assembly.Load这无疑是present组装?我的、异常、这无疑、FileNotFoundException

2023-09-06 14:14:03 作者:轉角之後,開始念你

在我的Windows Azure角色C#code我做到以下几点:

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

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

FileNotFoundException异常被抛出。问题是这样的名称程序集是present和code以上运行之前甚至已经加载 - 我看到在调试器对应的线输出窗口,当我做的:

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"));

的结果是,如果我用其中(),然后的SingleOrDefault( )我得到一个引用相应的组装对象。

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

为什么我不能加载与程序集的Assembly.Load()

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

推荐答案

这load()的调用只能当Microsoft.WindowsAzure.ServiceRuntime.dll存储在您的应用程序的探测路径成功。默认情况下,相同的目录EXE。问题是,它不被存储在那里,它被存储在GAC

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.

海关总署的一点是,作为具有相同名称但不同的[的AssemblyVersion] S,文化和处理器架构组件的存款。这与你的load()的问题,你不指定任何。有没有合理的方式融合可以选择装配你,这是容易给你错了。因此,它不打扰,哪怕只有一个来接。

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.

指定全AsssemblyName.FullName是必需的。利用项目+添加引用避免的。

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