¿Por qué devolver BAD IL FORMAT al ensamblaje de carga del servicio wcf?

I want to load this Biblioteca de clases :

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

Tengo un servicio wcf which returns to me a byte[] matrizClassLibrary1) 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);

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


Could not load file or assembly '4096 bytes loaded from Client2, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.


Bad IL format

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

preguntado el 10 de marzo de 12 a las 08:03

4096 bytes sounds suspiciously like some WCF limit, perhaps something similar to this; fudofuad.wordpress.com/2010/04/26/… -

Could we see the server side code reading/sending the assembly? -

2 Respuestas

The first thing to check in this scenario is that the byte[] you received is exactamente 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.

respondido 10 mar '12, 08:03

@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

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

@Yahia I didn't suspect that you did - it was directed at whoever did (as evidenced by the -1 score) - marc gravell

@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

@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 sí logró help (I.e. the contents were not identical), then great! But back to the point: was the byte[] recibido idéntico 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

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. Corral sfc en el sistema
  3. Corral chkdsk
  4. Compare the byte streams of assemblies (do this first if you're loading an assembly from a byte stream)

contestado el 15 de mayo de 12 a las 20:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.