c# client-server class cast error.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexx12
    New Member
    • Apr 2014
    • 1

    #1

    c# client-server class cast error.

    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 :

    Code:
    // 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.St udent cannot be cast to [B]ServerClient.St udent.
    >Type A originates from 'Server, Version=0.0.0.0 , Culture=neutral , PublicKeyToken= null' in >the context 'Default' at location >D:\Faculty\Wor kspace\C#\Serve rClient\ServerC lient\Server.ex e'. Type B originates from >Client, Version=0.0.0.0 , Culture=neutral , PublicKeyToken= null' in the context 'Default' at >location 'D:\Faculty\Wor kspace\C#\Serve rClient\ServerC lient\Client.ex e'.

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

    Code:
    `Student s1 = (Student)bin.Deserialize(receive.BaseStream);`
    with
    Code:
        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:

    Code:
    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.
Working...