一..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
usingSystem;2
usingSystem.Collections;3
usingSystem.Text;4

5
namespaceSimpleRemoting6


{7
publicclassHelloServer:MarshalByRefObject8


{9
publicHelloServer()10


{11

/**////输出信息,服务器激活12
Console.WriteLine("服务器激活……");13
}14
publicStringHelloMethod(Stringname)15


{16
Console.WriteLine(17
"服务器端:{0}",name);18
return"这里是:"+name;19
}20
}21
}
2.创建宿主应用程序:
注册通道
注册服务器激活的远程对象
运行宿主程序
usingSystem;2
usingSystem.Net;3
usingSystem.Runtime.Remoting;4
usingSystem.Runtime.Remoting.Channels;5
usingSystem.Runtime.Remoting.Channels.Tcp;6
usingSystem.Runtime.Remoting.Channels.Http;7

8
namespaceSimpleRemoting9


{10

11
publicclassServer12


{13
publicstaticintMain(string[]args)14


{15

16

/**////创建Tcp通道17
TcpChannelchan1=newTcpChannel(8085);18

19

/**////创建Http通道20
HttpChannelchan2=newHttpChannel(8086);21

22

/**////注册通道23
ChannelServices.RegisterChannel(chan1);24
ChannelServices.RegisterChannel(chan2);25

26
RemotingConfiguration.RegisterWellKnownServiceType27
(28
typeof(HelloServer),29
"SayHello",30
WellKnownObjectMode.Singleton31
);32

33

34
System.Console.WriteLine("按任意键退出!");35

/**////下面这行不能少36
System.Console.ReadLine();37
return0;38
}39

40
}41
}42

43
3.建立客户端程序:
注册通道
根据URL得到对象代理
使用代理调用远程对象
usingSystem;2
usingSystem.Runtime.Remoting;3
usingSystem.Runtime.Remoting.Channels;4
usingSystem.Runtime.Remoting.Channels.Tcp;5
usingSystem.Runtime.Remoting.Channels.Http;6
usingSystem.IO;7

8
namespaceSimpleRemoting9


{10
publicclassClient11


{12
publicstaticvoidMain(string[]args)13


{14

/**////使用TCP通道得到远程对象15
TcpChannelchan1=newTcpChannel();16
ChannelServices.RegisterChannel(chan1);17

18
HelloServerobj1=(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
HttpChannelchan2=newHttpChannel();30
ChannelServices.RegisterChannel(chan2);31

32
HelloServerobj2=(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
"ClientTCPHelloMethod{0}",45
obj1.HelloMethod("Caveman1"));46
Console.WriteLine(47
"ClientHTTPHelloMethod{0}",48
obj2.HelloMethod("Caveman2"));49
Console.ReadLine();50
}51
}52
}53
.NET Remoting 是一种使对象能在不同的应用程序域或不同计算机之间进行通信的技术。本文介绍了 .NET Remoting 的基本原理,涵盖了远程对象的概念、激活方式、通道、编组和监听等内容,并通过一个实例演示了如何实现 .NET Remoting 的开发过程。
280

被折叠的 条评论
为什么被折叠?



