Serializing/Deserializing an object
-
...and I fail at it :sigh: And somehow I can't get a working answer on Google. I want to store an in-memory generated assembly in the database for future use. I create this assembly from generated C#. For some reason, I can serialize the object, it generates a nice byte[] which is stored in the db. But when I want to retrieve the assembly for deserialisation to my object, I get an error like
"{"Unable to find assembly '9-onzk69, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."}" My serialization routine is:public byte[] Formula2ByteArray(Compiler.CompiledFormula formula)
{
byte[] result = null;try
{
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();formatter.Serialize(mStream, formula); result = mStream.ToArray();
}
catch (System.Exception ex)
{
throw ex;
}return result;
}And my deserialisation code is:
public CompiledFormula LoadFormulaFromAssembly(byte[] array)
{
try
{
Compiler.CompiledFormula formula;using (System.IO.MemoryStream stream = new System.IO.MemoryStream(array)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formula = (CompiledFormula)formatter.Deserialize(stream); } return formula;
}
catch (System.Exception ex)
{
throw ex;
}
}Both the application which saves it into the db and the application which uses it contain the same libraries, so the objects-structures are known to both apps. De serialisation/deserialisation routines are in the same library/class working with the same object model. What am I missing? If I do something stupid, just tell me :)
The consumer isn't a moron; she is your wife.
-
...and I fail at it :sigh: And somehow I can't get a working answer on Google. I want to store an in-memory generated assembly in the database for future use. I create this assembly from generated C#. For some reason, I can serialize the object, it generates a nice byte[] which is stored in the db. But when I want to retrieve the assembly for deserialisation to my object, I get an error like
"{"Unable to find assembly '9-onzk69, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."}" My serialization routine is:public byte[] Formula2ByteArray(Compiler.CompiledFormula formula)
{
byte[] result = null;try
{
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();formatter.Serialize(mStream, formula); result = mStream.ToArray();
}
catch (System.Exception ex)
{
throw ex;
}return result;
}And my deserialisation code is:
public CompiledFormula LoadFormulaFromAssembly(byte[] array)
{
try
{
Compiler.CompiledFormula formula;using (System.IO.MemoryStream stream = new System.IO.MemoryStream(array)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formula = (CompiledFormula)formatter.Deserialize(stream); } return formula;
}
catch (System.Exception ex)
{
throw ex;
}
}Both the application which saves it into the db and the application which uses it contain the same libraries, so the objects-structures are known to both apps. De serialisation/deserialisation routines are in the same library/class working with the same object model. What am I missing? If I do something stupid, just tell me :)
The consumer isn't a moron; she is your wife.
The answer might be stupid as well :) Just a thought, if you need to Serialize an object shouldnt that be marked with the attribute "[Serializable]"?
Hariharan.T
-
The answer might be stupid as well :) Just a thought, if you need to Serialize an object shouldnt that be marked with the attribute "[Serializable]"?
Hariharan.T
-
The relevant classes are marked as Serializable, as otherwise the error would be notifying me which class is not serializable. This is not the case however, thus the serialize-attribute is set correctly.
The consumer isn't a moron; she is your wife.
Can you modify your deserialization code a bit to narrow down where an exception is thrown? i mean seperate try catch for deserializing and one more for the type casting to find when it fails
Hariharan.T
-
Can you modify your deserialization code a bit to narrow down where an exception is thrown? i mean seperate try catch for deserializing and one more for the type casting to find when it fails
Hariharan.T
The exception is thrown on the line: "formula = (CompiledFormula)formatter.Deserialize(stream);" This is the only deserializationcode I have, since I let the framework handle it. I'm suspecting the name '9-onzk69' is the name of the temporary file created by the codeprovider which compiles it, but I don't know for sure, as a system wide file search comes up empty. The stack trace of the exception is:
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at Compiler.CompilerSingleton.LoadFormuleFromAssembly(Byte[] array)The InnerException is null.
The consumer isn't a moron; she is your wife.