i try to send Image Object as serialized File from Client to Server by @TCP
And get this exception

Server Code
namespace Receiver
{
[Serializable()]
public class ImageSerial : ISerializable
{
public Image img = null;
public ImageSerial(SerializationInfo info, StreamingContext ctxt)
{
img = (Image)info.GetValue("OMG", typeof(Image));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("OMG", img);
}
}
public class ObjSerial
{
private Stream stream;
private BinaryFormatter bformatter;
private string FILENAME = "m.bin";
ImageSerial mp = new ImageSerial();
public Image getImgFromBin()
{
stream = File.Open(FILENAME, FileMode.Open);
bformatter = new BinaryFormatter();
mp = (ImageSerial)bformatter.Deserialize(stream);
stream.Close();
return mp.img;
}
Client code
namespace WindowsFormsApplication5
{
[Serializable()]
class ImageSerial :ISerializable
{
public Image img = null;
public ImageSerial() { }
public ImageSerial(SerializationInfo info, StreamingContext ctxt)
{
img = (Image)info.GetValue("OMG", typeof(Image));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("OMG",img);
}
}
public class ObjSerial
{
private string FILENAME = "m.bin";
private TcpClient tcpClient;
private FileStream fstFile;
private NetworkStream strRemote;
private string SERVERIP = "10.102.239.207";
private int SERVERPort = 5051;
public void start(Image ims)
{
ImageSerial mp = new ImageSerial();
mp.img = ims;
Stream stream = File.Open(FILENAME, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, mp);
stream.Close();
//Clear mp for further usage.
sendFile();
}
private void ConnectToServer(string ServerIP, int ServerPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(ServerIP, ServerPort);
}
catch (Exception exMessage)
{
// Display any possible error
}
}
private void sendFile()
{
ConnectToServer(SERVERIP, SERVERPort);
if (tcpClient.Connected == false)
{
ConnectToServer(SERVERIP, SERVERPort);
}
strRemote = tcpClient.GetStream();
fstFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strRemote.Write(downBuffer, 0, bytesSize);
}
tcpClient.Close();
strRemote.Close();
fstFile.Close();
}
}
i have read many topic about this exception and all of them talk about two solution
- formatter.Binder
- AppDomain.CurrentDomain.AssemblyResolve
but still not work
Of course the server side will not find the client side assembly. And it should not. Your client side code must not live in the server side. The problem here is that you defined your
ImageSerialclass twice, one in the server and one in the client. That’s plain wrong if you control both sides. Create a common assembly that is referenced by both the client and the server and put your common classes there.Also, remove all references from the server to the client. It should be the other way bound if you wish, or use an intermediate service layer such as WCF.