I am using .NET smart card, it has the same concepts of .NET remoting.
So my smartcard (as a server) has this service:
public class MyService : MarshalByRefObject { string a = "abC"; public byte[] MySampleMethod() { MyService obj = new MyService(); return help.ObjectToByteArray( obj); }}}
and this is ObjectToByteArray(obj)
public static byte[] ObjectToByteArray(MyService obj) { if (obj == null) return null; BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(0); bf.Serialize(ms, obj); return ms.ToArray(); }
As for client :
public static void Main() { // create and register communication channel APDUClientChannel channel = new APDUClientChannel(); ChannelServices.RegisterChannel(channel); // get the referenc to remote object MyService service = (MyService)Activator.GetObject(typeof(MyService), URL); // invoke the remote method byte[] result = service.MySampleMethod(); MyService obj = ByteArrayToObject(result); Console.WriteLine(result[0]); Console.ReadLine(); // unregister the communication channel ChannelServices.UnregisterChannel(channel); }
ByteArrayToObject
public static MyService ByteArrayToObject(byte[] arrBytes) { MemoryStream memStream = new MemoryStream(0); BinaryFormatter binForm = new BinaryFormatter(); memStream.Write(arrBytes, 0, arrBytes.Length); memStream.Seek(0, SeekOrigin.Begin); //memStream.Position = 0; MyService obj = (MyService)binForm.Deserialize(memStream); return obj; }
The problem is when I want to Deserialize the object.
I test this string "ABCDE" , serialize it in the card and the result hex is :
WHILE the result when i serialize it on my pc is :
So on my PC application, deserializing the second one works well, but when i deserialize the first string (from smart card) I got :
"The input stream is not a valid binary format. The starting contents (in bytes) are: 1C-5D-D2-00-27-11-02-00-00-00-05-00-00-00-05-00-00 ..."