.net remoting(1)简单例子

1.例子(程序间的通讯)

    class Program
    {
        static void Main(string[] args)
        {
            HttpChannel _channel = new HttpChannel(10001);
            ChannelServices.RegisterChannel(_channel, false);
            Console.WriteLine("http 通道remoting服务开始……");
            //方法 RegisterWellKnownServiceType 将服务器上的对象类型注册为已知类型
            RemotingConfiguration.RegisterWellKnownServiceType
                (typeof(selfRemoteObject), "selfRemoteObject",
                WellKnownObjectMode.Singleton);
            //服务器激活的对象有两种激活模式:Singleton和SingleCall,这两种模式又叫已知对象,由枚举类型:WellKnownObjectMode来标识。
            //Singleton类型在任一时刻只有一个实例。所有的客户端请求都将由这个实例提供服务。如果不存在实例,则服务器创建一个,且所有的后来的客户端请求都将由这个实例提供服务。对于单件类型,会关联到默认的生存期。
            //SingleCall类型针对每个客户端请求创建一个实例。下一个方法调用将由其他服务器实例提供服务,即使在系统尚未回收前一个实例的引用的情况下也是这样。
            Test();
            Console.Read();
        }


        public static void Test()
        {
            //通过Actovator.GetObject方法来获取代理
            selfRemoteObject app =
            (selfRemoteObject)Activator.GetObject(typeof(selfRemoteObject),
            "http://localhost:10001/selfRemoteObject");
            Console.WriteLine(app.Plus(1, 3));
            Console.ReadLine();
        }
    }

    public class selfRemoteObject : MarshalByRefObject
    {
        public int Plus(int a, int b)
        {
            Console.WriteLine("客户端请求调用:a={0},b={1}", a, b);
            Console.WriteLine("计算结果:a+b={0},返回给客户端调用", a + b);
            return a + b;
        }
    }

 

原文地址:https://www.cnblogs.com/lgxlsm/p/7506158.html