Remoting例子

1、服务器端的代码

1-1 Service对象

public class Singer:MarshalByRefObject
    {
        public string Sing(string str)
        {
            return "i'm sing..." + str;
        }
    }

1-2 宿主

 class Program
    {
        static void Main(string[] args)
        {
            //创建一个通道用来公布对外的服务
            TcpChannel channel = new TcpChannel(8888);
            //将tcp通道通过通道服务进行注册,这里的false只的是不使用iis的安全机制
            ChannelServices.RegisterChannel(channel,false);

            //通过远程配置对象将要公布的对象的进行公布,包括类型uri
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Singer), "Singer", WellKnownObjectMode.Singleton);

            Console.Read();
        }
    }

 

2、客户端的代码

         //这里使用Activitor对象来创建远程对象的引用
            Singer singer = (Singer)Activator.GetObject(typeof(Singer),"tcp://localhost:8888/Singer");
            //调用远程对象的引用的方法
            string content = singer.Sing("haha");
            MessageBox.Show(content);

3、注意要引入的命名空间 服务端

using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

posted @ 2013-03-02 23:48  feidaochuanqing  阅读(131)  评论(0编辑  收藏  举报