Assembly.Load() cause BadImageFormatException

  • Thread starter Thread starter V&G
  • Start date Start date
V

V&G

FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
Byte[] body = new Byte[(int)fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Assembly assembly = Assembly.Load(body);

This code cause BadImageFormatException. An error message is:
"A BadImageFormatException has been thrown while parsing the signature.
This is likely due to lack of a generic context. Ensure
genericTypeArguments and genericMethodArguments are provided and
contain enough context."
InnerException:
"An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)"

My target is to load an assembly and serialize it for future proposes.
So I can not use LoadFrom() directly.

Do anybody have any suggestions?
Thanks for all
 
If CLR finds a file to load and , but the file is not a managed
assembly , a BadImageFormatException is thrown.
 
FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
Byte[] body = new Byte[(int)fs.Length];
fs.Read(buffer, 0, buffer.Length);

There's no guarantee that a single call to Read will fill the byte
array. You should check the return value and if necessary keep calling
Read until everything has been read or the end of the stream is
reached.

My target is to load an assembly and serialize it for future proposes.
So I can not use LoadFrom() directly.

But does it load correctly if you use Assembly.LoadFrom(fileName) so
you know that the file isn't corrupt or anything?


Mattias
 
I checked it. The whole file was red.
The error occurs on particular assemblies.
May be the reason is than these assemblies contain unmanaged code?
 
V&G said:
My target is to load an assembly and serialize it for future proposes.
So I can not use LoadFrom() directly.

Do anybody have any suggestions?


Read what Gregory Beamer said in another post in this group. It makes no
sense to 'serialize an assembly'. What are you trying to gain from
serializing an assembly as opposed to loading the assembly from disk?

Richard
 
There is a multi-user application - shell for accumulating/initializing
components for future usage. In other words user can choose some
assembly, choose some object type from that assembly, instantiate that
object and initialize the instance with default values. After all, all
of this information should be saved to the database for the future
usage. As you see I need to load all referenced assemblies also.
Because the application is a multi-user application I need to store in
database all required data including locally loaded assemblies, so
other users would be able to recreate my AppDomain assemblies
collection to instantiate the objects. Of cause, the alternative is to
manage some centralized data store on server disk, but in this case
there is a danger to get inconsistent data in two sources:
"database"-"disk storage".
Here is the problems start...
As you see, I have opened three different topics in this subj. As I
understand, there are two general ways to load an assembly:
- by defining assembly path/name
- by reading assembly library as a byte array
And here is the source of my questions. I can load assemblies in first
way. But there is no way to serialize an assembly loaded to the memory.
On the other hand, I can read assembly to byte array and load it from
there. But in this case CLR does not recognizes assemblies loaded from
COFF-file image as signed and creates multiple entries for the same
assembly in AppDomain (see
http://groups.google.com/group/micr...amework/browse_thread/thread/6dff95d4ea9b6671)...
So I wanted to serialize the object loaded in correct way.
 
Back
Top