1

I want to load this Class library :


namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
        }
        public static int Sum(int a, int b)
        {
            return a + b;
        }
    }
}

I have a wcf service which returns to me a byte[] array (ClassLibrary1) i can not load this assembly

static void Main(string[] args)
{
    FileTransferService.ApplicationHostServiceClient client = new FileTransferService.ApplicationHostServiceClient();

    FileTransferService.AssemblyPackage[] asmbs = client.GetFile();
    //var newDomain = AppDomain.CreateDomain("FooBar", null, null);
    foreach (FileTransferService.AssemblyPackage item in asmbs) 
    {
        byte[] mybuffer = item.Buffer;
        new AssemblyLoader().LoadAndCall(mybuffer);
    }
}

public class AssemblyLoader : MarshalByRefObject
{
    public void LoadAndCall(byte[] binary)
    {
        Assembly loadedAssembly = AppDomain.CurrentDomain.Load(binary);
        object[] tt = { 3, 6 };
        Type typ = loadedAssembly.GetType("ClassLibrary1.Class1");
        MethodInfo minfo = typ.GetMethod("Sum", BindingFlags.Public);
        int x = (int)minfo.Invoke(null, tt);
        Console.WriteLine(x);
    }
}

Error return to me in this method : Assembly loadedAssembly = AppDomain.CurrentDomain.Load(binary);

ERROR:

BADIMAGEFORMAT EXCEPTION
Could not load file or assembly '4096 bytes loaded from Client2, 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.

EXCEPTION :

Bad IL format

i have googling this kind of error but no exact solution. i want to load my assembly using AppDomain.

mihai
  • 2,746
  • 3
  • 35
  • 56
loki
  • 2,926
  • 8
  • 62
  • 115

2 Answers2

2

The first thing to check in this scenario is that the byte[] you received is exactly identical to the original, as there are many ways of making a mess of handing a chunk of binary. Perhaps write the file to disk (File.WriteAllBytes) and your favourite file compare tool, or use something like base-64 or a sha-1 hash to validate the contents. I strongly suspect you'll find it isn't the same.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • @Yahia I never said it was; but since it is text-based, and since the file should be short for such a simple example, it should be easy to eyeball two base-64 strings to see if they are obviously different. – Marc Gravell Mar 10 '12 at 08:28
  • 3
    Nice, a downvote for trying to teach someone to fish. However, I stand by the answer: until the OP follows up with "I compared the files, they were identical", this remains the most obvious thing to look at, and changes the nature of the problem. Importantly, instead of googling "BAD IL FORMAT" (which isn't the problem), they'll know to google "file being truncated over WCF", or maybe posting their "send" code for us to took at and diagnose. – Marc Gravell Mar 10 '12 at 09:11
  • @Yahia I didn't suspect that you did - it was directed at whoever did (as evidenced by the -1 score) – Marc Gravell Mar 10 '12 at 10:25
  • @Marc: i voted to you -1. But i checked accepted answer. Why do you talk about accepted . Your answer is not related to my question. Not related to WCF. i dislike your answer .your answer may mislead another developer. you can not give ALL TİME correct answer. You can misunderstand or i can not tell my problem correctly. There are upvote aslso downvote. Dont be accepted – loki Mar 10 '12 at 13:06
  • @programmerist I'm very confused: firstly, nobody mentioned "accepted"; now, either the answer helps or it doesn't. If it didn't help, then don't mark it as an accepted answer. If it *did* help (I.e. the contents were not identical), then great! But back to the point: was the `byte[]` received **identical** to the original file, yes or no? If yes, then fine - let us all know that; if no, then that very much is directly related to the problem. – Marc Gravell Mar 10 '12 at 15:55
1

Since this is one of the first results when googling Bad IL format I thought I'd explain what that means.

BadImageFormatException is thrown when the Intermediate Language of the assembly is invalid. In the case of this question it was due to WCF truncating it, in my case a .Net Framework dll was corrupted by a failing harddrive.

So in general the problem will exist at the byte level, for this problem in general I'd debug it with these steps:

  1. Recompile everything possible
  2. Run sfc on the system
  3. Run chkdsk
  4. Compare the byte streams of assemblies (do this first if you're loading an assembly from a byte stream)
Thymine
  • 8,775
  • 2
  • 35
  • 47