Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 e.g. "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.

Is there a generic way to provide a fallback assembly when an assembly can't be loaded?

like image 904
Kirill Osenkov Avatar asked Oct 02 '09 03:10

Kirill Osenkov


2 Answers

I think you can just call assembly.LoadFrom to load the assembly of your choice with practically no security checks. 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.

like image 137
Aaron Fischer Avatar answered Oct 10 '22 04:10

Aaron Fischer


What triggers the load attempt? IOW do you call Assembly.Load or this is a result of type resolution attempt? 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.

If you load the Assembly manually though make sure you load it with Assembly.Load - not Assembly.LoadFrom. There are subtle differences in type resolution depending on what context assembly is loaded into

like image 34
mfeingold Avatar answered Oct 10 '22 02:10

mfeingold