.NET Remoting编程三 实现客户端激活对象

 在例子一中,会存在3个问题:
1.创建通道,对象注册和其他配置代码过多,如果这些细节改变,则需要重新编译
2.对于已知(Well-Known)对象,只能通过默认的构造函数来创建对象,客户端不能指定非默认的构造函数
3.客户端必须引用包含已知类型的程序集,因此需要把这个程序集部署到每个客户端上,违背了分布式的原则

本节将解决问题2

首先定义一个简单的类Customer,实现了一个非默认的构造函数和一个SayHello方法
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  MathLibrary
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Customer 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class Customer:MarshalByRefObject
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        string mName;
11InBlock.gif        //注意,是带参数的构造函数
12InBlock.gif        public Customer(string name)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            //
15InBlock.gif            // TODO: 在此处添加构造函数逻辑
16InBlock.gif            //
17InBlock.gif            Console.WriteLine("Customer.ctor({0}",name);
18InBlock.gif            mName = name;
19ExpandedSubBlockEnd.gif        }

20InBlock.gif        public string SayHello()
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            Console.WriteLine("Customer.SayHello()");
23InBlock.gif            return "Hello"+mName;
24ExpandedSubBlockEnd.gif        }

25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}
服务器端代码:
注意用到RemotingConfiguration.RegisterActivatedServiceType()方法注册服务
 1 None.gif using  System;
 2 None.gif using  System.Runtime.Remoting;
 3 None.gif using  System.Runtime.Remoting.Channels;
 4 None.gif using  System.Runtime.Remoting.Channels.Http;
 5 None.gif using  MathLibrary;
 6 None.gif namespace  MathServer
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// ServerMain 的摘要说明。
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class ServerMain
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public ServerMain()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            //
16InBlock.gif            // TODO: 在此处添加构造函数逻辑
17InBlock.gif            //
18ExpandedSubBlockEnd.gif        }

19InBlock.gif        public static void Main(string[] args)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            HttpChannel channel = new HttpChannel(13101);
22InBlock.gif            ChannelServices.RegisterChannel(channel);
23InBlock.gif            RemotingConfiguration.RegisterActivatedServiceType(typeof(MathLibrary.Customer));
24InBlock.gif
25InBlock.gif            Console.WriteLine("ServerStarted.Press Enter to Enddot.gifdot.gifdot.gif.");
26InBlock.gif            Console.ReadLine();
27ExpandedSubBlockEnd.gif        }

28ExpandedSubBlockEnd.gif    }

29ExpandedBlockEnd.gif}
客户端代码:
 1 None.gif using  System;
 2 None.gif using  System.Runtime.Remoting;
 3 None.gif using  System.Runtime.Remoting.Channels;
 4 None.gif using  System.Runtime.Remoting.Channels.Http;
 5 None.gif using  MathLibrary;
 6 None.gif namespace  MathClient
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// ClientMain 的摘要说明。
10ExpandedSubBlockEnd.gif    /// </summary>
11InBlock.gif    public class ClientMain
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public ClientMain()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            //
16InBlock.gif            // TODO: 在此处添加构造函数逻辑
17InBlock.gif            //
18ExpandedSubBlockEnd.gif        }
19InBlock.gif        public static void Main(string[] args)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            RemotingConfiguration.RegisterActivatedClientType(typeof(MathLibrary.Customer),"http://localhost:13101");
22InBlock.gif
23InBlock.gif            Customer cust = new Customer("Homer");
24InBlock.gif            Console.WriteLine(cust.SayHello());
25InBlock.gif            Console.ReadLine();
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28ExpandedBlockEnd.gif}
执行结果如下:
服务器端执行情况:
Image00008.gif
客户端情况:
Image00012.gif

转载于:https://www.cnblogs.com/fastcatcher/archive/2005/08/14/214592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值