初识用.NET Remoting来开发分布式应用

一..NET Remoting简介:

.NET Remoting从某种意义上讲是DCOM的替代品。ASP.NET Web服务十分有用,但是这项技术在企业内联网的解决方案中,对于某些业务请求来说并不快,也没有足够的灵活性,而且,ASP.NET Web服务需要有运行时的支持。使用.NET Remoting技术后,可以将Web服务提供给世界上的任何地方。而且可以在所有的应用程序类型中运行Web服务。

二..NET Remoting 的基本原理:

体系结构图如下:

Remoting01.JPG

三.几个重要的概念:

1.远程对象:

远程对象类是从MarshalByRefObject类中派生的。跨越应用程序域调用这个类需要使用代理。.NET Remoting支持两种类型的远程对象:知名的(Well-known)远程对象和客户激活(Client-activated)远程对象。远程对象其实包括两层含义:

操作远程对象:对象运行在远程,客户段向他发送消息;

传递远程对象:将远程对象拿到本地,或者将本地对象发送过去,对副本进行操作。

2.激活:

使用new运算符可以激活远程对象。还有其它一些方式也可以激活远程对象,在以后的随笔里面我会介绍。

3.通道:

一个远程对象使用通道发送和接收消息。服务器选择一个通道来监听请求,客户端选择通道来和服务器通讯。Remoting提供了内置的通道:TCP通道和HTTP通道,我们也可以编写自己的通道。

4.编组:

数组通过应用程序域被传递的过程称为编组。将变量作为远程对象的参数来发送时,这个变量必须被转换,以便能够通过应用程序域发送该变量。

5.监听:

使用监听,能够将某些功能置入到方法调用链中。如果调用某个对象的方法,监听层便能够捕获调用来转换方法调用,或是完成某些日志记录。.NET Remoting调用链的每一部分都是用监听。

四.开发Remoting三步走:

开发.NET Remoting分三步走,在这里以一个简单的例子来说明。

1.创建远程对象:

继承System.MarshalByRefObject

1 None.gifusingSystem;
2None.gifusingSystem.Collections;
3None.gifusingSystem.Text;
4None.gif
5None.gifnamespaceSimpleRemoting
6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
7InBlock.gifpublicclassHelloServer:MarshalByRefObject
8ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
9InBlock.gifpublicHelloServer()
10ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif/**////输出信息,服务器激活
12InBlock.gifConsole.WriteLine("服务器激活……");
13ExpandedSubBlockEnd.gif}

14InBlock.gifpublicStringHelloMethod(Stringname)
15ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
16InBlock.gifConsole.WriteLine(
17InBlock.gif"服务器端:{0}",name);
18InBlock.gifreturn"这里是:"+name;
19ExpandedSubBlockEnd.gif}

20ExpandedSubBlockEnd.gif}

21ExpandedBlockEnd.gif}


2.创建宿主应用程序:

注册通道

注册服务器激活的远程对象

运行宿主程序

1 None.gifusingSystem;
2None.gifusingSystem.Net;
3None.gifusingSystem.Runtime.Remoting;
4None.gifusingSystem.Runtime.Remoting.Channels;
5None.gifusingSystem.Runtime.Remoting.Channels.Tcp;
6None.gifusingSystem.Runtime.Remoting.Channels.Http;
7None.gif
8None.gifnamespaceSimpleRemoting
9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
10InBlock.gif
11InBlock.gifpublicclassServer
12ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
13InBlock.gifpublicstaticintMain(string[]args)
14ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
15InBlock.gif
16ExpandedSubBlockStart.gifContractedSubBlock.gif/**////创建Tcp通道
17InBlock.gifTcpChannelchan1=newTcpChannel(8085);
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif/**////创建Http通道
20InBlock.gifHttpChannelchan2=newHttpChannel(8086);
21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif/**////注册通道
23InBlock.gifChannelServices.RegisterChannel(chan1);
24InBlock.gifChannelServices.RegisterChannel(chan2);
25InBlock.gif
26InBlock.gifRemotingConfiguration.RegisterWellKnownServiceType
27InBlock.gif(
28InBlock.giftypeof(HelloServer),
29InBlock.gif"SayHello",
30InBlock.gifWellKnownObjectMode.Singleton
31InBlock.gif);
32InBlock.gif
33InBlock.gif
34InBlock.gifSystem.Console.WriteLine("按任意键退出!");
35ExpandedSubBlockStart.gifContractedSubBlock.gif/**////下面这行不能少
36InBlock.gifSystem.Console.ReadLine();
37InBlock.gifreturn0;
38ExpandedSubBlockEnd.gif}

39InBlock.gif
40ExpandedSubBlockEnd.gif}

41ExpandedBlockEnd.gif}

42None.gif
43None.gif


3.建立客户端程序:

注册通道

根据URL得到对象代理

使用代理调用远程对象

1 None.gifusingSystem;
2None.gifusingSystem.Runtime.Remoting;
3None.gifusingSystem.Runtime.Remoting.Channels;
4None.gifusingSystem.Runtime.Remoting.Channels.Tcp;
5None.gifusingSystem.Runtime.Remoting.Channels.Http;
6None.gifusingSystem.IO;
7None.gif
8None.gifnamespaceSimpleRemoting
9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
10InBlock.gifpublicclassClient
11ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
12InBlock.gifpublicstaticvoidMain(string[]args)
13ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif/**////使用TCP通道得到远程对象
15InBlock.gifTcpChannelchan1=newTcpChannel();
16InBlock.gifChannelServices.RegisterChannel(chan1);
17InBlock.gif
18InBlock.gifHelloServerobj1=(HelloServer)Activator.GetObject(
19InBlock.giftypeof(SimpleRemoting.HelloServer),
20InBlock.gif"tcp://localhost:8085/SayHello");
21InBlock.gif
22InBlock.gifif(obj1==null)
23ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
24InBlock.gifSystem.Console.WriteLine(
25InBlock.gif"连接TCP服务器失败");
26ExpandedSubBlockEnd.gif}

27InBlock.gif
28ExpandedSubBlockStart.gifContractedSubBlock.gif/**////使用HTTP通道得到远程对象
29InBlock.gifHttpChannelchan2=newHttpChannel();
30InBlock.gifChannelServices.RegisterChannel(chan2);
31InBlock.gif
32InBlock.gifHelloServerobj2=(HelloServer)Activator.GetObject(
33InBlock.giftypeof(SimpleRemoting.HelloServer),
34InBlock.gif"http://localhost:8086/SayHello");
35InBlock.gif
36InBlock.gifif(obj2==null)
37ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
38InBlock.gifSystem.Console.WriteLine(
39InBlock.gif"连接HTTP服务器失败");
40ExpandedSubBlockEnd.gif}

41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif/**////输出信息
43InBlock.gifConsole.WriteLine(
44InBlock.gif"ClientTCPHelloMethod{0}",
45InBlock.gifobj1.HelloMethod("Caveman1"));
46InBlock.gifConsole.WriteLine(
47InBlock.gif"ClientHTTPHelloMethod{0}",
48InBlock.gifobj2.HelloMethod("Caveman2"));
49InBlock.gifConsole.ReadLine();
50ExpandedSubBlockEnd.gif}

51ExpandedSubBlockEnd.gif}

52ExpandedBlockEnd.gif}

53None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值