Dynamic code compilation and serialization

G

Guest

Hello-

I'm working on a system that manages manufacturer data. From time to
time, we send information to a manufacturer. The method can vary widely.
What I'm attempting is to add two fields to the manufacturer's database
record. The first field is NotificationCode. The second is
NotificationAssemblyCache. Custom C# code can be written and put in the
first field. When it comes time to send a notification, the system will
compile the code into an assembly and execute the custom notification method.
I also want to serialize the in-memory compiled assembly to the
NotificationAssemblyCache field. So far, everything works great. I can
retrieve, compile, execute, and serialize the custom code. The problem is
that I can't seem to deserialize the assembly. Here's a snippet of my code
(it serializes to a file in this example):

//SERIALIZE
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream saveStream = new FileStream("c:\\assembly.bin", FileMode.Create);
binaryFormatter.Serialize(saveStream, assembly);
saveStream.Close();

//DESERIALIZE
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream loadStream = new FileStream("c:\\assembly.bin", FileMode.Open);
Assembly assembly = (Assembly)binaryFormatter.Deserialize(loadStream);
loadStream.Close();

I get the following error on the third line of the deserialization snippet:

Unhandled Exception: System.Runtime.Serialization.SerializationException:
Insufficient state to deserialize the object. More information is needed.

Does anyone know how to prevent this error?

Regards-
Eric
 
O

Otis Mukinfus

Hello-

I'm working on a system that manages manufacturer data. From time to
time, we send information to a manufacturer. The method can vary widely.
What I'm attempting is to add two fields to the manufacturer's database
record. The first field is NotificationCode. The second is
NotificationAssemblyCache. Custom C# code can be written and put in the
first field. When it comes time to send a notification, the system will
compile the code into an assembly and execute the custom notification method.
I also want to serialize the in-memory compiled assembly to the
NotificationAssemblyCache field. So far, everything works great. I can
retrieve, compile, execute, and serialize the custom code. The problem is
that I can't seem to deserialize the assembly. Here's a snippet of my code
(it serializes to a file in this example):

//SERIALIZE
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream saveStream = new FileStream("c:\\assembly.bin", FileMode.Create);
binaryFormatter.Serialize(saveStream, assembly);
saveStream.Close();

//DESERIALIZE
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream loadStream = new FileStream("c:\\assembly.bin", FileMode.Open);
Assembly assembly = (Assembly)binaryFormatter.Deserialize(loadStream);
loadStream.Close();

I get the following error on the third line of the deserialization snippet:

Unhandled Exception: System.Runtime.Serialization.SerializationException:
Insufficient state to deserialize the object. More information is needed.

Does anyone know how to prevent this error?

Regards-
Eric

Eric,

This answer is a 100% WAG, but in your code, do you have any
"[NonSerialized]" attributes assigned to code elements that would be
required to reconstruct the object(s) you are serializing?


Otis Mukinfus
http://www.otismukinfus.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top