初识用.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.gif using  System;
 2 None.gif using  System.Collections;
 3 None.gif using  System.Text;
 4 None.gif
 5 None.gif namespace  SimpleRemoting
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    public class HelloServer : MarshalByRefObject
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public HelloServer()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////输出信息,服务器激活
12InBlock.gif            Console.WriteLine("服务器激活……");
13ExpandedSubBlockEnd.gif        }

14InBlock.gif        public String HelloMethod(String name)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            Console.WriteLine(
17InBlock.gif                "服务器端 : {0}", name);
18InBlock.gif            return "这里是:" + name;
19ExpandedSubBlockEnd.gif        }

20ExpandedSubBlockEnd.gif    }

21ExpandedBlockEnd.gif}


2.创建宿主应用程序:

注册通道

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

运行宿主程序

 1 None.gif using  System;
 2 None.gif using  System.Net;
 3 None.gif using  System.Runtime.Remoting;
 4 None.gif using  System.Runtime.Remoting.Channels;
 5 None.gif using  System.Runtime.Remoting.Channels.Tcp;
 6 None.gif using  System.Runtime.Remoting.Channels.Http;
 7 None.gif
 8 None.gif namespace  SimpleRemoting 
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10InBlock.gif
11InBlock.gif    public class Server
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public static int Main(string [] args) 
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            
16ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////创建Tcp通道
17InBlock.gif            TcpChannel chan1 = new TcpChannel(8085);
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////创建Http通道
20InBlock.gif            HttpChannel chan2 = new HttpChannel(8086);
21InBlock.gif            
22ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////注册通道
23InBlock.gif            ChannelServices.RegisterChannel(chan1);
24InBlock.gif            ChannelServices.RegisterChannel(chan2);
25InBlock.gif
26InBlock.gif            RemotingConfiguration.RegisterWellKnownServiceType
27InBlock.gif                (
28InBlock.gif                typeof(HelloServer),
29InBlock.gif                "SayHello",
30InBlock.gif                WellKnownObjectMode.Singleton
31InBlock.gif                );
32InBlock.gif            
33InBlock.gif
34InBlock.gif            System.Console.WriteLine("按任意键退出!");
35ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////下面这行不能少
36InBlock.gif            System.Console.ReadLine();
37InBlock.gif            return 0;
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}

42 None.gif
43 None.gif


3.建立客户端程序:

注册通道

根据URL得到对象代理

使用代理调用远程对象

 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.Tcp;
 5 None.gif using  System.Runtime.Remoting.Channels.Http;
 6 None.gif using  System.IO;
 7 None.gif
 8 None.gif namespace  SimpleRemoting 
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10InBlock.gif    public class Client
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        public static void Main(string[] args)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////使用TCP通道得到远程对象
15InBlock.gif            TcpChannel chan1 = new TcpChannel();
16InBlock.gif            ChannelServices.RegisterChannel(chan1);
17InBlock.gif
18InBlock.gif            HelloServer obj1 = (HelloServer)Activator.GetObject(
19InBlock.gif                typeof(SimpleRemoting.HelloServer),
20InBlock.gif                "tcp://localhost:8085/SayHello");
21InBlock.gif
22InBlock.gif            if (obj1 == null)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                System.Console.WriteLine(
25InBlock.gif                    "连接TCP服务器失败");
26ExpandedSubBlockEnd.gif            }

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

41InBlock.gif            
42ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////输出信息
43InBlock.gif            Console.WriteLine(
44InBlock.gif                "ClientTCP HelloMethod {0}",
45InBlock.gif                obj1.HelloMethod("Caveman1"));
46InBlock.gif            Console.WriteLine(
47InBlock.gif                "ClientHTTP HelloMethod {0}",
48InBlock.gif                obj2.HelloMethod("Caveman2"));
49InBlock.gif            Console.ReadLine();
50ExpandedSubBlockEnd.gif        }

51ExpandedSubBlockEnd.gif    }

52ExpandedBlockEnd.gif}

53 None.gif


结束语:初识用.NET Remoting来开发分布式应用就到这里了,有时间我会就.NET Remoting技术写成系列文章。包括基于租约的生存期,编组,异步远程调用等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值