Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
I am developing a client server application in C#.

I am sending from my server to client an object of `Student` type which I serialize on the server side and deserialize on client side. The serialization and deserialization are as follows :

// server side serialization.
Student s1 = new Student("Chuck","Noris");
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(writer.BaseStream, s1);

// client side deserialization.
1.BinaryFormatter bin = new BinaryFormatter();
2.Student s1 = (Student)bin.Deserialize(receive.BaseStream);
3.Console.WriteLine(s1.ToString());
and i get this error at runtime:
>[A]ServerClient.Student cannot be cast to [B]ServerClient.Student.
>Type A originates from 'Server, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in >the context 'Default' at location >D:\Faculty\Workspace\C#\ServerClient\ServerClient\Server.exe'. Type B originates from >Client, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at >location 'D:\Faculty\Workspace\C#\ServerClient\ServerClient\Client.exe'.

Also, if on the client side on line 2 I replace:

`Student s1 = (Student)bin.Deserialize(receive.BaseStream);`

with

Object s1 = bin.Deserialize(receive.BaseStream);
Console.WriteLine(s1.ToString());

everything works just fine, and the WriteLine method is printing toString represetation of object send it from server.

To create the Server.exe and Client.exe I used the following commands:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Server.cs Repository.cs Student.cs
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Client.cs Student.cs
and I get no errors or warning.
Also my `Student` class have the `[Serialization]` attribute.
I did searched the web for this kind of errors but i couldn't find anything usefull


Later Edit(adding more code).After simplyfing my code so that it just send an serialize object from server to client where that object is deserilizeed, looks as follows:

//server side -sending part
Student s1 = new Student(23, "alex", "dimofte", "Sergent Hategan", "23.04.2013");
     List> note = new List>();
     Tuple n1 = new Tuple("Analiza", 8);
     Tuple n2 = new Tuple("Algebra", 4);
     Tuple n3 = new Tuple("Romana", 9);
     Tuple n4 = new Tuple("Sport", 8);
     Tuple n5 = new Tuple("Analiza", 8);
     Tuple n6 = new Tuple("Algebra", 4);

     note.Add(n1);
     note.Add(n2);
     note.Add(n3);
     note.Add(n4);
     note.Add(n5);
     note.Add(n6);

     s1.Grades = note;
     BinaryFormatter binaryFormatter = new BinaryFormatter();
     binaryFormatter.Serialize(writer.BaseStream, s1);

     writer.AutoFlush = true
;
//where writer is constructed :
StreamReader reader = new StreamReader(this.clientSocket.GetStream()); // clientSocket is the connected client

//client side -receiving part
C#
this.send.AutoFlush = true;

BinaryFormatter bin = new BinaryFormatter();
//Student s = (Student)bin.Deserialize(receive.BaseStream); // this don't work
Object s = bin.Deserialize(receive.BaseStream);  // this works
Console.WriteLine(s.ToString());  // with s of type Object it prints what is supposed
Console.ReadLine();
Posted
Updated 5-Apr-14 11:29am
v3
Comments
Homero Rivera 5-Apr-14 17:07pm    
Sure there is not a second edition of Student class in Client.cs ?
How about namespace; same namespaces?
Why should Server use Repository.cs and Student not use that?
More code from those classes could be useful.

Here is a silly question (I mean mine, yours is good)... You are sending the 'student' over network/internet right?
BTW, very interesting question!
Member 10727293 5-Apr-14 17:19pm    
Hello and thank you for your interest in helping. I checked the namsepaces-no ploblem there,
I simplified the server code so that i don't use any Repository and still having the same error. I will update my question with more code.

1 solution

I hate to say it, but the cast on client side...
C#
Student s = (Student)bin.Deserialize(receive.BaseStream);

will never work, not like that at least.


Why? For security reasons... How about someone forging a "Server.Student" and send that to your Client with malicious data?
If your Client.Student makes intensive use of the Server.Student data, then they can really harm your computer.
That would be a very good reason not to accept ("Server" assembly).Student as ("Client" assembly).Student .
The .Net framework will not allow it.
If you was/were* Microsoft, and you didn't want the app's made with your technology to have their security compromised, you would probably put a lock somewhere.
That´ll cover the developer's bases for him.

Come think of it... A project is made by 50 developers, one gets angry with the company and thinks to himself, 'I´ll show them!' and he makes at home a Server.Student cause he knows Client.Student could be blown up with some data.
That can't be allowed to happen.



There is more information at:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/6bcc2f47-d307-4dc1-9c83-ded1d2630e44/cannot-cast-object-of-type-x-to-x?forum=netfxbcl[^]
There seems to be some goodies that can help this problem, like signatures and things like that which are mentioned in that link above.



One possible workaround, whenever you want to send data between applications is to send simple strings of data under a TCP controlled session.
When received, you can have some commands check that string for security and convert it to the data that you need in full control of the situation.
Or, use a generic Object like you have.


You can also try, but I can't asure this will work, to make a single application that has both Client and Server functionality and try installing the very same assembly in 2 different computers. That could work, but is just an idea.


*I´m not a native English speaker, I don't know which is right in that sentence: was/were? Can someone tell?
 
Share this answer
 
v5
Comments
Member 10727293 5-Apr-14 19:04pm    
Got it! Thank you very much for taking time to take a look at my problem and give some feedback and a workaround.Once again.. thank you!
Homero Rivera 5-Apr-14 19:29pm    
My pleasure! Your question actually tought me some things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900