c# client-server List of objects cast error -
i developing client server application in c#.
i sending server client list
of students
objects serialize on server side , deserialize on client side. serialization , deserialization follows :
// server side serialization. list<student> ordbymedgen = repo.ordbymedgen(); binaryformatter binaryformatter = new binaryformatter(); binaryformatter.serialize(writer.basestream, ordbymedgen); // client side deserialization. binaryformatter bin = new binaryformatter(); list<student> list = (list<student>)bin.deserialize(receive.basestream); console.writeline(list.count); // print list
using method succesfully send list
of strings server side , printed on client side. when tried student
class got following error
[a]system.collections.generic.list
1[serverclient.student] cannot cast [b]system.collections.generic.list
1[serverclient.student]. type originates 'mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089' in context 'loadneither' @ location 'c:\windows\microsoft.net\assembly\gac_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. type b originates 'mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089' in context 'loadneither' @ location 'c:\windows\microsoft.net\assembly\gac_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
to create server.exe , client.exe used 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 no errors or warning. student
class have [serialization]
attribute. did searched web kind of errors couldn't find usefull.
later edit: maybe @ compilation fact student.cs provided reference both on server , client somehow confuse compiler , sees student 2 different types in error: , b ?
later edit(2): problem can solved creating @ compilation dll student.cs , referenced on server , client, compiler doesen't confused.
it sounds have 2 different assemblies - 1 @ client, 1 on server - contributing type. types defined assembly, , if identical: not interchangeable. basically, binaryformatter fight every step of way here. advice: don't use binaryformatter. other serializers (xmlserializer, json.jet, jil, datacontractserializer, protobuf-net etc) work fine in scenario, , not complain @ all.
Comments
Post a Comment