Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem loading assembly from byte[]

Problem loading assembly from byte[]

Scheduled Pinned Locked Moved C#
helpdatabasecsharpsql-servervisual-studio
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Helfdane
    wrote on last edited by
    #1

    Hi, I've got this issue with loading an assembly from a byte array. What happens? First I create an assembly from C#-code which is generated. Then I stream the assembly to a byte[] and store this array in the database (column type = image, MSSQL 2000). When I need the assembly, I retrieve the byte[] from the db and want to load it. I try to load the assembly using Assembly.Load(array), and then it breaks. For some strange reason my VS decided to give me a Dutch error while my language is set to English: {"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Poging om een programma te laden met een onjuiste indeling."} Which I think translates to: {"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."} I'm having trouble finding a good solution for it, as I don't know where it goes wrong. It all happens in the same namespace, by the same package. So the creation, streaming and loading happens in the same dll. Source I use is below:

    public CompiledFormula GetCompiledFormula(string cstext)
    {
    // Create assembly by compile
    Assembly ass = Compile(cstext);

    CompiledFormula result = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");
    return result;
    }

    The byte conversion as follows:

    public byte[] GetArrayFromAssembly(Assembly formula)
    {
    byte[] assembly;

    try
    {
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    formatter.Serialize(stream, formula);
    assembly = stream.ToArray();

    }
    catch(Exception ex)
    {
    throw ex;
    }
    }

    And this is the loading routine:

    public CompiledFormula LoadFormuleFromAssembly(byte[] array)
    {
    try
    {
    Assembly assembly = Assembly.Load(array);

    CompiledFormula formule = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");

    return formule;
    }
    catch (System.Exception ex)
    {
    throw ex;
    }
    }

    Anyone has an idea? Also, tell me if I make a horrible mistake here ;)

    The consumer isn't a moron; she is your wife.

    R 1 Reply Last reply
    0
    • H Helfdane

      Hi, I've got this issue with loading an assembly from a byte array. What happens? First I create an assembly from C#-code which is generated. Then I stream the assembly to a byte[] and store this array in the database (column type = image, MSSQL 2000). When I need the assembly, I retrieve the byte[] from the db and want to load it. I try to load the assembly using Assembly.Load(array), and then it breaks. For some strange reason my VS decided to give me a Dutch error while my language is set to English: {"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Poging om een programma te laden met een onjuiste indeling."} Which I think translates to: {"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."} I'm having trouble finding a good solution for it, as I don't know where it goes wrong. It all happens in the same namespace, by the same package. So the creation, streaming and loading happens in the same dll. Source I use is below:

      public CompiledFormula GetCompiledFormula(string cstext)
      {
      // Create assembly by compile
      Assembly ass = Compile(cstext);

      CompiledFormula result = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");
      return result;
      }

      The byte conversion as follows:

      public byte[] GetArrayFromAssembly(Assembly formula)
      {
      byte[] assembly;

      try
      {
      using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
      {
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
      new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

      formatter.Serialize(stream, formula);
      assembly = stream.ToArray();

      }
      catch(Exception ex)
      {
      throw ex;
      }
      }

      And this is the loading routine:

      public CompiledFormula LoadFormuleFromAssembly(byte[] array)
      {
      try
      {
      Assembly assembly = Assembly.Load(array);

      CompiledFormula formule = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");

      return formule;
      }
      catch (System.Exception ex)
      {
      throw ex;
      }
      }

      Anyone has an idea? Also, tell me if I make a horrible mistake here ;)

      The consumer isn't a moron; she is your wife.

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      I would be hugely suspicious of your serialisation code. A 169 byte assembly sounds very, very small to me. Unfortunately, I have no idea how to convert a dynamically created assembly to a byte array (short of using AssemblyBuilder.Save then loading it in from disc). Not being much help I know, but I can't believe you can use BinaryFormatter.Serialize to save the MSIL of assembly to a stream like that.

      Regards, Rob Philpott.

      H 1 Reply Last reply
      0
      • R Rob Philpott

        I would be hugely suspicious of your serialisation code. A 169 byte assembly sounds very, very small to me. Unfortunately, I have no idea how to convert a dynamically created assembly to a byte array (short of using AssemblyBuilder.Save then loading it in from disc). Not being much help I know, but I can't believe you can use BinaryFormatter.Serialize to save the MSIL of assembly to a stream like that.

        Regards, Rob Philpott.

        H Offline
        H Offline
        Helfdane
        wrote on last edited by
        #3

        The assembly created, as it is now, is a 1 function class which on disk is no more than a 10k .cs file. The dynamic part is the function body which is generated. For me, I think the problem can either be in the byte array generation (the point of your suspicion), or at the load routines. I'm still considering namespaces to be an issue too, but haven't gotten any proof of me being right or wrong. The hunt continues...

        The consumer isn't a moron; she is your wife.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups