简体   繁体   English

如何提供后备程序集而不是无法加载的程序集?

[英]How to provide a fallback assembly instead of the one that can't be loaded?

At runtime, if a referenced assembly fails to load with eg "Strong name validation failed" (because it's test-signed), is there a way to provide a substitution assembly from another path that is real-signed? 在运行时,如果引用的程序集无法加载,例如“强名称验证失败”(因为它是经过测试签名的),是否有办法从另一个实际签名的路径提供替换程序集?

I tried subscribing to AppDomain.CurrentDomain.AssemblyResolve, but it doesn't get fired, because the "bad" assembly technically exists, it just can't be loaded. 我尝试订阅AppDomain.CurrentDomain.AssemblyResolve,但它没有被触发,因为“坏”程序集技术上存在,它只是无法加载。

Is there a generic way to provide a fallback assembly when an assembly can't be loaded? 在无法加载程序集时,是否存在提供回退程序集的通用方法?

What triggers the load attempt? 是什么触发了加载尝试? IOW do you call Assembly.Load or this is a result of type resolution attempt? 你怎么称呼Assembly.Load或这是类型解析尝试的结果? If it is the latter you can try to play with the AppDomain TypeResolve event, if the former - you can add additional logic to your call to the Assembly.Load. 如果是后者,您可以尝试使用AppDomain TypeResolve事件,如果是前者 - 您可以在调用Assembly.Load时添加其他逻辑。

If you load the Assembly manually though make sure you load it with Assembly.Load - not Assembly.LoadFrom. 如果您手动加载程序集,但确保使用Assembly.Load加载它 - 而不是Assembly.LoadFrom。 There are subtle differences in type resolution depending on what context assembly is loaded into 类型分辨率存在细微差别,具体取决于加载的上下文程序集

I think you can just call assembly.LoadFrom to load the assembly of your choice with practically no security checks. 我想你可以调用assembly.LoadFrom加载你选择的程序集,几乎没有安全检查。 We us this a lot at the start of our app so we can better deal with other assemblies version change. 我们在应用程序开始时就这么做了很多,所以我们可以更好地处理其他程序集版本更改。

Also look at Assembly.LoadFrom Method (String, Evidence, Byte[], AssemblyHashAlgorithm) looks like you can control passing in the hash as well as the hash algorithm. 另请参阅Assembly.LoadFrom方法(String,Evidence,Byte [],AssemblyHashAlgorithm)看起来您可以控制传递哈希以及哈希算法。

Looks like what I want is impossible. 看起来我想要的是不可能的。 I decided to go another route. 我决定走另一条路。 We'll have to modify the build system to conditionally link to signed binaries instead of test-signed binaries at compile time. 我们必须修改构建系统,以便在编译时有条件地链接到已签名的二进制文件而不是测试签名的二进制文件。

Thanks everyone for the suggestions though! 谢谢大家的建议!

there is a standard way to find an assembly in case the application fails to do so: 在应用程序无法执行此操作时,有一种标准方法可以找到程序集:

// register on assembly resolve exception
AppDomain.CurrentDomain.AssemblyResolve += ResolveEventHandler;

// try to load the assembly yourself
private static Assembly ResolveEventHandler(object sender, ResolveEventArgs args)
{
    return Assembly.Load(some_location);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM