SerializationException during Deserialization of a class

G

Guest

I am serializing the following struct

public struct Tan

int x
int y


using the following code

BinaryFormatter formatter = new BinaryFormatter()
MemoryStream mStream = new MemoryStream()
formatter.Serialize(mStream, tank)

and sending this to a UdpClient on a different machine using this code

IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), 11000)
UdpClient client = new UdpClient()
client.Send(mStream.GetBuffer(), (int)mStream.Length, remoteEP)

Now then, my client is receiving the packet via the following cod
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), 11000)
UdpClient client = new UdpClient(localEP)
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 11000)

BinaryFormatter formatter = new BinaryFormatter()
MemoryStream mStream = new MemoryStream()
byte[] message = client.Receive(ref remoteEP)
mStream.Write(message, 0, message.Length)
mStream.Position = 0

At this point, I compare the mStream that I sent to the mStream that arrived (breakpoints) and I see that they are in fact the same size, have the same bytes writting to them, etc. - they look absultely correct. However, the following command causes the error in question

Tank tank = (Tank)formatter.Deserialize(mStream)

//The Tank structure is declared both on the server and the client and they look the exact same, etc . .

I get the following error
System.Runtime.Serialization.SerializationException: Cannot find the assembly MyClient, Version=1.0.1600.39535, Culture=neutral, PublicKeyToken=null
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly(
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(Binar
etc.... etc.... etc...

MyClient is the assembly that SENT the original packet. Why is the MyServer assembly (the packet receiver) throwing an error about the sending machines assembly? I'm only trying to serialize a class and I'm baffled as to why assembly information has anything to do with deserializing it - but then again, I'm new to Serialization :

I've been struggling with this now for 3 days and my deadline is tommorrow. I sure hope I can find an answer soon :

Thanks in advance for any replies

Rob
 
K

Kurbangaliev Denis V.

You need...

[Serializable]

public struct Tank
{
int x;
int y;
}



Rob Walker said:
I am serializing the following struct:

public struct Tank
{
int x;
int y;
}

using the following code:

BinaryFormatter formatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
formatter.Serialize(mStream, tank);

and sending this to a UdpClient on a different machine using this code:

IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), 11000);
UdpClient client = new UdpClient();
client.Send(mStream.GetBuffer(), (int)mStream.Length, remoteEP);

Now then, my client is receiving the packet via the following code
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), 11000);
UdpClient client = new UdpClient(localEP);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 11000);

BinaryFormatter formatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
byte[] message = client.Receive(ref remoteEP);
mStream.Write(message, 0, message.Length);
mStream.Position = 0;

At this point, I compare the mStream that I sent to the mStream that
arrived (breakpoints) and I see that they are in fact the same size, have
the same bytes writting to them, etc. - they look absultely correct.
However, the following command causes the error in question:
Tank tank = (Tank)formatter.Deserialize(mStream);

//The Tank structure is declared both on the server and the client and
they look the exact same, etc . . .
I get the following error:
System.Runtime.Serialization.SerializationException: Cannot find the
assembly MyClient, Version=1.0.1600.39535, Culture=neutral,
PublicKeyToken=null.
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembl
y()
System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(Binary
etc.... etc.... etc....

MyClient is the assembly that SENT the original packet. Why is the
MyServer assembly (the packet receiver) throwing an error about the sending
machines assembly? I'm only trying to serialize a class and I'm baffled as
to why assembly information has anything to do with deserializing it - but
then again, I'm new to Serialization :)
I've been struggling with this now for 3 days and my deadline is
tommorrow. I sure hope I can find an answer soon :(
 
K

Ken Kolda

I sounds like you have two versions of your struct -- one in the MyClient
assembly and one in MyServer. Because the assembly name from which an object
comes from is part of its identity, these are completely different objects,
even if you gave them the same name, namespace, etc.

The solution is to put these kinds of shared obejcts in a separate assembly
and include it on both client and server machines.

Ken


Rob Walker said:
I do have [Serializable] decalred - I knew I was going to leave out a
detail :) I tried to be as detailed as possible but it never fails :)
 
G

Guest

Thanks Ken, you are abosultely right

I didn't want to have to put these objects that I want to serialize into a seperate assembly and include this assembly on both the client and the server, hence why I never explored this option. However, I couldn't take the punishment of trying to find another work around any longer. :

I appreciate the feedback and I finally have my netcode working.
 

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