Search code examples
c#embedded-resource

Load Embedded Assembly


I am trying to load an embedded assembly at run-time on a server-side DLL. The assembly I embedded is my own code.

I am getting an Unverifiable code failed policy check error when trying to load the assembly.

Is there any way around this?

Here is the code:

    byte[] resource = null;

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
    {
        if (stream != null)
        {
            resource = new byte[stream.Length];
            stream.Read(resource, 0, (int)stream.Length);
        }
    }

    AppDomain.CurrentDomain.Load(resource);

EDIT:

A bit of a shot in the dark, but I tried to save the DLL to disk first, and load it that way. Here is what I get now: Could not load file or assembly or one of its depenencies. The given assembly or codebase was invalid.


Solution

  • Found a solution, if anyone stumbles on this post:

    -C# will not allow you to load an assembly from bytes unless you are in a high-security context.

    -Funny enough, if you have the permissions to save to file first, and then use Assembly.Load(filename) (I made the mistake of using AppDomain.CurrentDomain.Load(filename)) it will have no problem loading the assembly.