views:

62

answers:

1

I'm going to give an example of using System.Data.SQLite.DLL which is a mixed assembly with unmanaged code: If I execute this :

  var assembly= Assembly.LoadFrom("System.Data.SQLite.DLL")

No exceptions are thrown, but if I do this :

  var rawAssembly = File.ReadAllBytes("System.Data.SQLite.DLL");
  var assembly = Assembly.Load(rawAssembly);

The CLR throws a FileLoadException with "Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)". Let's say I'm trying to load this assembly on a child AppDomain, how can I customize the AppDomain's security to allow me pass the policy check?

+2  A: 

We are the victim of a crummy exception message. Loading assemblies with Assembly.Load(byte[]) that contain unmanaged code in not supported. This is the subject of this feedback item.

Hans Passant
Could you give an example on how to create an appropriate evidence object?
Thiado de Arruda
Have you tried using the AppDomain.Current.Evidence?
Hans Passant
I haven't, but isn't that just a copy of the current domain's security settings(the one that threw the exception in the first place) ?
Thiado de Arruda
I meant the primary AppDomain's evidence. How did you initialize the AppDomain? Note the CreateDomain(string, Evidence) overload.
Hans Passant
I created the secondary domain with 'AppDomain.CreateDomain("ChildDomain,null,setup)"'(the only option I change in the setup is the application base dir)As I said, it doesnt matter if I load the raw assembly from the primary or child domain, it will always throw that exception, thats why I'm asking how to change the security settings to allow me to load mixed assemblies from byte arrays(if you showed a code snippet that made my code work that would be great);
Thiado de Arruda
@Thiado: started looking deeper when I got the same exception in full trust. Found the answer, post updated.
Hans Passant
Thanks for your research :)
Thiado de Arruda