使用Assembly.LoadFrom和的Assembly.Load组件怪异的行为时,混合装载 [英] Weird behaviour when mixing loading of assemblies using Assembly.LoadFrom and Assembly.Load
问题描述
怪异的行为混合装载使用Assembly.LoadFrom和组件的Assembly.Load时
使用的Assembly.Load加载与Assembly.LoadFrom集会,后来,当我遇到了怪异的行为。
我加载使用Assembly.LoadFrom,其中组件位于一个文件夹,这是不执行文件夹中的程序集。
后来在我的测试code,当我试图向大会再次装入的Assembly.Load,负载失败,出现System.IO.FileNotFoundException(无法加载文件或程序集...),尽管事实已加载的程序集。加载失败都与强名称和非强名称(当初之所以装再度本次大会是的BinaryFormatter的使用)。
然而,如果该组件位于后面负载成功在两种情况下执行的文件夹,具有强名称和非强名称。在这种情况下,你可以看到两个相同的组件是由两个不同的位置加载。
一个简单的code样品,再现这个问题 -
大会assembly1 = Assembly.LoadFrom(@C:\ A.DLL);
//加载具有较强的名称失败 大会assembly2 =的Assembly.Load(@一,版本= 1.0.0.0, 文化=中性公钥= 14986c3f172d1c2c);
//还装载有非强失败 大会assembly3 =的Assembly.Load(@A);
- 为什么CLR忽略已加载的程序集? 任何解释
- 我不知道如何能解决这个问题?
感谢。
这不是奇怪。根据该文件,加载与负载和LoadFrom将放置在不同环境下的程序集。 这可能的帮助。
- 为什么CLR忽略已加载的程序集?
任何解释
由于他们在不同的上下文。
- 我不知道如何能解决这个问题?
这是同样的情况下加载,或者帮助在CLR查找程序集,也许是通过附加一个处理程序 AppDomain.AssemblyResolve
。
另类
如果您是从装载组件的位置是AppDomain.BaseDirectory下的子目录下,你可以简单的条目添加到您的App.config:
<结构>
<运行>
< assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
<探测privatePath =斌; BIN2 \ subbin; BIN3/>
< / assemblyBinding>
< /运行>
< /结构>
http://msdn.microsoft.com/en-us/library/ 823z9h8w.aspx
Weird behavior when mixing loading of assemblies using Assembly.LoadFrom and Assembly.Load
I have encountered a weird behavior when loading assemblies with Assembly.LoadFrom and later on with Assembly.Load.
I am loading an assembly using Assembly.LoadFrom, where the assembly is located in a folder which is not the execution folder.
Later on in my test code when I try to load once again this assembly with Assembly.Load, the load fails with a System.IO.FileNotFoundException ("Could not load file or assembly…") despite the fact that the assembly is already loaded. The load fails both with the strong name and the non-strong name (the original reason for loading once again this assembly is a usage of a BinaryFormatter).
However, in case the assembly is located in the execution folder the later load succeeds in both cases, with the strong name and the non-strong name. In this case you can see that two identical assemblies are loaded from two different locations.
A simple code sample that recreates this problem –
Assembly assembly1 = Assembly.LoadFrom(@"C:\a.dll");
// Loading with a strong-name fails Assembly assembly2 = Assembly.Load(@"a, Version=1.0.0.0, Culture=neutral, PublicKeyToken=14986c3f172d1c2c");
// Also loading with a non-strong fails Assembly assembly3 = Assembly.Load(@"a");
- Any explanation why the CLR ignores the already loaded assembly?
- Any idea how can I alleviate this problem?
Thanks.
That's not weird. As per the documentation, loading with Load and LoadFrom will place the assemblies in different contexts. This might help.
- Any explanation why the CLR ignores the already loaded assembly?
Because they're in a different context.
- Any idea how can I alleviate this problem?
Load from the same context, or help the CLR find the assembly, perhaps by attaching a handler to AppDomain.AssemblyResolve
.
Alternative
If the location you are loading assemblies from is a subfolder under AppDomain.BaseDirectory you can simply add an entry to your App.config:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
http://msdn.microsoft.com/en-us/library/823z9h8w.aspx
这篇关于使用Assembly.LoadFrom和的Assembly.Load组件怪异的行为时,混合装载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!