Remoting示例

通过Remoting,客户端可以借助远程对象的代理对象,完成服务端的操作,下面是一个最初的demo,深入学习可以参考链接:

http://www.cnblogs.com/xia520pi/archive/2011/11/02/2233371.html

服务端:

static void Main(string[] args)
        {
            //在服务器端创建TcpServerChannel信道  
            var channel = new TcpServerChannel(8086);

            //注册该信道,使之可用于远程对象。  
            ChannelServices.RegisterChannel(channel, true);

            //用于为服务器激活的对象注册远程对象类型,(把知名的远程对象类型注册为 RemotingServices)  
            //第一个参数是 typeof(Hello),它指定远程对象的类型。第二个参数 Hi 是远程对象的 URI, 客户端访问远程对象时要使用这个 URI。 最后一个参数是远程对象的模式。             
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);

            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }  

客户端:

 static void Main(string[] args)
        {
            Console.WriteLine("Press return after the server is started");
            Console.ReadLine();

            //在客户端创建TcpServerChannel信道  
            ChannelServices.RegisterChannel(new TcpClientChannel(), true);

            //GetObject()它调用Remoting Services.Connect()方法以 返回远程对象的代理对象。第一个参数指定远程对象的类型。第二个参数是远程对象的URL。  
            Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");
            if (obj == null)
            {
                Console.WriteLine("could not locate server");
                return;
            }
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(obj.Greeting("Stephanie"));
            }
            Console.ReadLine();
        }  
原文地址:https://www.cnblogs.com/sulong/p/6354393.html