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

三.几个重要的概念:
1.远程对象:
远程对象类是从MarshalByRefObject类中派生的。跨越应用程序域调用这个类需要使用代理。.NET Remoting支持两种类型的远程对象:知名的(Well-known)远程对象和客户激活(Client-activated)远程对象。远程对象其实包括两层含义:
操作远程对象:对象运行在远程,客户段向他发送消息;
传递远程对象:将远程对象拿到本地,或者将本地对象发送过去,对副本进行操作。
2.激活:
使用new运算符可以激活远程对象。还有其它一些方式也可以激活远程对象,在以后的随笔里面我会介绍。
3.通道:
一个远程对象使用通道发送和接收消息。服务器选择一个通道来监听请求,客户端选择通道来和服务器通讯。Remoting提供了内置的通道:TCP通道和HTTP通道,我们也可以编写自己的通道。
4.编组:
数组通过应用程序域被传递的过程称为编组。将变量作为远程对象的参数来发送时,这个变量必须被转换,以便能够通过应用程序域发送该变量。
5.监听:
使用监听,能够将某些功能置入到方法调用链中。如果调用某个对象的方法,监听层便能够捕获调用来转换方法调用,或是完成某些日志记录。.NET
Remoting调用链的每一部分都是用监听。
四.开发Remoting三步走:
开发.NET Remoting分三步走,在这里以一个简单的例子来说明。
1.创建远程对象:
继承System.MarshalByRefObject
继承System.MarshalByRefObject
1 using System;
2 using System.Collections;
3 using System.Text;
4
5 namespace SimpleRemoting
6  {
7 public class HelloServer : MarshalByRefObject
8 {
9 public HelloServer()
10 {
11 /**////输出信息,服务器激活
12 Console.WriteLine("服务器激活……");
13 }
14 public String HelloMethod(String name)
15 {
16 Console.WriteLine(
17 "服务器端 : {0}", name);
18 return "这里是:" + name;
19 }
20 }
21 }
2.创建宿主应用程序:
注册通道
注册服务器激活的远程对象
运行宿主程序
1 using System;
2 using System.Net;
3 using System.Runtime.Remoting;
4 using System.Runtime.Remoting.Channels;
5 using System.Runtime.Remoting.Channels.Tcp;
6 using System.Runtime.Remoting.Channels.Http;
7
8 namespace SimpleRemoting
9  {
10
11 public class Server
12 {
13 public static int Main(string [] args)
14 {
15
16 /**////创建Tcp通道
17 TcpChannel chan1 = new TcpChannel(8085);
18
19 /**////创建Http通道
20 HttpChannel chan2 = new HttpChannel(8086);
21
22 /**////注册通道
23 ChannelServices.RegisterChannel(chan1);
24 ChannelServices.RegisterChannel(chan2);
25
26 RemotingConfiguration.RegisterWellKnownServiceType
27 (
28 typeof(HelloServer),
29 "SayHello",
30 WellKnownObjectMode.Singleton
31 );
32
33
34 System.Console.WriteLine("按任意键退出!");
35 /**////下面这行不能少
36 System.Console.ReadLine();
37 return 0;
38 }
39
40 }
41 }
42
43
3.建立客户端程序:
注册通道
根据URL得到对象代理
使用代理调用远程对象
1 using System;
2 using System.Runtime.Remoting;
3 using System.Runtime.Remoting.Channels;
4 using System.Runtime.Remoting.Channels.Tcp;
5 using System.Runtime.Remoting.Channels.Http;
6 using System.IO;
7
8 namespace SimpleRemoting
9  {
10 public class Client
11 {
12 public static void Main(string[] args)
13 {
14 /**////使用TCP通道得到远程对象
15 TcpChannel chan1 = new TcpChannel();
16 ChannelServices.RegisterChannel(chan1);
17
18 HelloServer obj1 = (HelloServer)Activator.GetObject(
19 typeof(SimpleRemoting.HelloServer),
20 "tcp://localhost:8085/SayHello");
21
22 if (obj1 == null)
23 {
24 System.Console.WriteLine(
25 "连接TCP服务器失败");
26 }
27
28 /**////使用HTTP通道得到远程对象
29 HttpChannel chan2 = new HttpChannel();
30 ChannelServices.RegisterChannel(chan2);
31
32 HelloServer obj2 = (HelloServer)Activator.GetObject(
33 typeof(SimpleRemoting.HelloServer),
34 "http://localhost:8086/SayHello");
35
36 if (obj2 == null)
37 {
38 System.Console.WriteLine(
39 "连接HTTP服务器失败");
40 }
41
42 /**////输出信息
43 Console.WriteLine(
44 "ClientTCP HelloMethod {0}",
45 obj1.HelloMethod("Caveman1"));
46 Console.WriteLine(
47 "ClientHTTP HelloMethod {0}",
48 obj2.HelloMethod("Caveman2"));
49 Console.ReadLine();
50 }
51 }
52 }
53
结束语:初识用.NET Remoting来开发分布式应用就到这里了,有时间我会就.NET
Remoting技术写成系列文章。包括基于租约的生存期,编组,异步远程调用等等。
|