Problem during deserialization
-
Hi All, I am unable to deserialize the byte array to the object of structure on another machine,when i send it through TCP/IP by socket then there is no problem in serialization.,but problem occurs at the other side during deserialization. It shows the error something like "unable to find assembly....." I am using the following code.... private byte[] ObjectToByteArray(Object obj) { if (obj == null) return null; BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, obj); return ms.ToArray(); } // Convert a byte array to an Object private Object ByteArrayToObject(byte[] arrBytes) { MemoryStream memStream = new MemoryStream(arrBytes); BinaryFormatter binForm = new BinaryFormatter(); memStream.Position = 0; //memStream.Write(arrBytes, 0, arrBytes.Length); // memStream.Seek(0, SeekOrigin.Begin); Object obj = (Object)binForm.Deserialize(memStream); return obj; } Please help,it is urgent.... Regards Lalit Narayan
-
Hi All, I am unable to deserialize the byte array to the object of structure on another machine,when i send it through TCP/IP by socket then there is no problem in serialization.,but problem occurs at the other side during deserialization. It shows the error something like "unable to find assembly....." I am using the following code.... private byte[] ObjectToByteArray(Object obj) { if (obj == null) return null; BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, obj); return ms.ToArray(); } // Convert a byte array to an Object private Object ByteArrayToObject(byte[] arrBytes) { MemoryStream memStream = new MemoryStream(arrBytes); BinaryFormatter binForm = new BinaryFormatter(); memStream.Position = 0; //memStream.Write(arrBytes, 0, arrBytes.Length); // memStream.Seek(0, SeekOrigin.Begin); Object obj = (Object)binForm.Deserialize(memStream); return obj; } Please help,it is urgent.... Regards Lalit Narayan
That's telling you that it needs to know about the type (defined in an assembly you don't have loaded) in order to deserialise it. Make sure its in your project's references on the deserialisation side.
Regards, Rob Philpott.
-
That's telling you that it needs to know about the type (defined in an assembly you don't have loaded) in order to deserialise it. Make sure its in your project's references on the deserialisation side.
Regards, Rob Philpott.
-
Hi Rob, thanks for reply, but i have not attached the assembly at my side because i have to only read the data that is coming through TCP channel from the controller. Please reply... Regards, Lalit Narayan
What you have there is a method to convert an object to a byte array, and a method to convert a byte array to an object. What you're transmitting is the member data of the object. All the methods and code for it isn't sent - that's defined in an assembly. You need to make sure that for any type which is sent this way, the assembly which contains it is on both client and server. Find out which assembly the type you are serialising is in. Then make sure that assembly is referenced in the project dependencies of the deserialising project.
Regards, Rob Philpott.
-
What you have there is a method to convert an object to a byte array, and a method to convert a byte array to an object. What you're transmitting is the member data of the object. All the methods and code for it isn't sent - that's defined in an assembly. You need to make sure that for any type which is sent this way, the assembly which contains it is on both client and server. Find out which assembly the type you are serialising is in. Then make sure that assembly is referenced in the project dependencies of the deserialising project.
Regards, Rob Philpott.
Hi Rob, Thanks for ur reply.. Actually the problem is that we are communicating the DDC through windows service,on DDC side there is no .Net assembly but the embeded type of work over there(like C++ type). Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side. But for testing purpose i am communicating between two computers that have .net application and windows service.But basically i have to set up communication between windows service and the DDC. So i do not want to attach the assembly..because there will no assembly on other side in real time. Please reply... Regards, Lalit Narayan
-
Hi Rob, Thanks for ur reply.. Actually the problem is that we are communicating the DDC through windows service,on DDC side there is no .Net assembly but the embeded type of work over there(like C++ type). Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side. But for testing purpose i am communicating between two computers that have .net application and windows service.But basically i have to set up communication between windows service and the DDC. So i do not want to attach the assembly..because there will no assembly on other side in real time. Please reply... Regards, Lalit Narayan
lnmca wrote:
Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side.
That's not good enough. When you call serialize, it packages up the fully qualified type name - that includes namespace, type name and assembly. When deserialising on the other side it will use all these things to create the type prior to populating it. Just because you have a type with the same name .NET will not use that. I don't entirely understand what's you trying to do, but unless both sides are .NET you want to steer clear of Serialisation and just send the relevant fields one by one.
Regards, Rob Philpott.
-
Hi Rob, Thanks for ur reply.. Actually the problem is that we are communicating the DDC through windows service,on DDC side there is no .Net assembly but the embeded type of work over there(like C++ type). Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side. But for testing purpose i am communicating between two computers that have .net application and windows service.But basically i have to set up communication between windows service and the DDC. So i do not want to attach the assembly..because there will no assembly on other side in real time. Please reply... Regards, Lalit Narayan
If on the other end you have a different type but with same fields then this how to deserialize: Advanced Binary Serialization: Deserializing an Object Into a Different Type Than the One It was Serialized Into[^]
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion