1

我正在使用 .NET 智能卡,它具有与 .NET 远程处理相同的概念。

所以我的智能卡(作为服务器)有这个服务:

public class MyService : MarshalByRefObject
{
     string a = "abC";

    public byte[] MySampleMethod()
    {
        MyService obj = new MyService();
        return help.ObjectToByteArray( obj);
    }}}

这是 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();
    }

至于客户:

    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);
    }

字节数组对象

    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;
    }

问题是当我想反序列化对象时。

我测试这个字符串 "ABCDE" ,在卡片中序列化它,结果十六进制是:

1C-5D-D2-00-27-11-02-00-00-00-05-00-00-00-05-00-00-00-01-41-00-42-00-43-00- 44-00‌​-45-00

当我在我的电脑上序列化它时的结果是:

00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-06-01-00-00-00-05-41-42- 43-44‌​-45-0B。

所以在我的 PC 应用程序上,反序列化第二个效果很好,但是当我反序列化第一个字符串(来自智能卡)时,我得到:

“输入流不是有效的二进制格式。起始内容(以字节为单位)为:1C-5D-D2-00-27-11-02-00-00-00-05-00-00-00-05- 00-00 ...”

4

1 回答 1

2

Gemalto.NET 智能卡仅支持按引用编组,因此您在服务器中拥有的任何原始类型和结构类型都可以在客户端中访问,而无需序列化,因为您已经通过远程调用获得了对对象的引用:

所以首先注册你的服务:

public class MyServer
    {
        /// <summary>
        /// specify the exposed remote object URI.
        /// </summary>
        private const string REMOTE_OBJECT_URI = "MyService.uri";

        /// <summary>
        /// Register the server onto the card.
        /// </summary>
        /// <returns></returns>
        public static int Main()
        {
            // Register the channel the server will be listening to.
            ChannelServices.RegisterChannel(new APDUServerChannel());

            // Register this application as a server            
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyService), REMOTE_OBJECT_URI, WellKnownObjectMode.Singleton);

            return 0;
        }
    }

然后定义服务类,您可以为 gemalto 文档返回原始类型和结构:

可以编组的类型包括基本值类型(byte、short、char、int、long、string 等)、结构、基本类型的数组和 MemoryStreams

public class MyService : MarshalByRefObject
    {
        public struct Person
        {
            public string name;
            public int id;

            public Person(int id, string name)
            {
                this.name = name;
                this.id = id;
            }

            public string getName()
            {
                return this.name;
            }

            public int getId()
            {
                return this.id;
            }
        }

        public string MySampleMethod()
        {
            return "This is return String";
        }

        public Person getPerson()
        {
            Person person = new Person(15, "Wajdy");
            return person;
        }
    }

现在在客户端应用程序中,您将拥有对服务对象的引用,并且您可以正常调用方法:

public class MyClient
    {
        private const string URL = "apdu://selfdiscover/MyService.uri";

        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);
            Console.WriteLine(service.MySampleMethod());

            MyService.Person person = service.getPerson();
            Console.WriteLine(person.getName());
            Console.WriteLine(person.getId());

            Console.ReadLine();

            // unregister the communication channel
            ChannelServices.UnregisterChannel(channel);
        }
    }
于 2013-04-09T08:00:03.610 回答