0

So I am trying to send a 'Packet' class over a network stream with the following code:

IFormatter formatter = new BinaryFormatter();
NetworkStream stream = client.GetStream();
formatter.Serialize(stream, packet);


stream.Flush();
stream.Close();
client.Close();

using this class:

[Serializable]
public class Packet
{

    public string header;
    public string content;
    public int size = 0;

    public Packet(string header, string content)
    {
        this.header = header;
        this.content = content;

        size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
    }
}

But I am getting the following error when reading on the other side:

'System.Runtime.Serialization.SerializationException: Unable to find assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

This is my reading code:

NetworkStream ns = client.GetStream();
IFormatter formatter = new BinaryFormatter();
Packet p = (Packet)formatter.Deserialize(ns);
MessageBox.Show(p.header);
return p;

Any idea why this is happening?

EDIT:

Server side Packet class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server
{

    public class Packet
    {
        public string header;
        public string content;
        public int size = 0;

        public Packet(string header, string content)
        {
            this.header = header;
            this.content = content;

            size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
        }
    }
}
5
  • What line throws the exception? Can you post the full code where you initialize "client?"
    – Chris A
    Feb 6, 2013 at 5:38
  • 'Packet p = (Packet)formatter.Deserialize(ns);' is where the error is being throwen. Feb 6, 2013 at 5:40
  • How are you sharing the Packet class (type) from your server side app with your client? Is it part of its own assembly?
    – Chris A
    Feb 6, 2013 at 5:44
  • The Packet class on my server is it's own assembly. Client/Server are separate projects. I will post that code now. Feb 6, 2013 at 5:45
  • You need to have the Packet class in a seperate class library. You need to reference this class library from the server and client projects. Make sure you don't use any other "Packet" classes defined in the client or the server projects. Feb 6, 2013 at 5:54

3 Answers 3

2

You can't binary serialize an object from one assembly and deserialize it against a class from a different assembly.

You need to have a third assembly that you reference from both the client and the server.

1
  • thanks :) I created a dll for both projects. Worked like a charm. Feb 6, 2013 at 5:59
1

When you deserialise from a BinaryFormatter the class must be available. This is what the error is saying.

I'm assuming that the Packet class is definied in the Client.dll. If so, then just at a reference to Client.dll in the "Server" project and remove the Packet definition in the server.

General practice is to have a DataModel assembly that can be shared with Client and Server.

Also if you use the XmlSerializer instead of BinaryFormatter, then you can have different implementations of the class on both client and server.

0

You have created two separate (although functionally identical) Packet classes, and your client cannot deserialize a different type than the one that was serialized, even if they have the same name and structure.

Try defining the Packet class in a separate third project/assembly of the Class Library type. Afterwards, reference that project or assembly from both your client and server. To be even more correct, you would define an Interface IPacket in this class library and simply implement it in both your client and server.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.