骆驼空间站

简单就是美,不受任何商业的驱使,我们有自己的圈子

博客园 首页 新随笔 联系 订阅 管理
本文介绍了Net Remoting的几个简单概念,并尝试从最简单的角度帮您理解什么是Remoting。同时,本文包括了一个使用Http Channel调用Remoting服务器的例子,并讨论了不同的Server对象的差别以及对象的生命周期。

  通过本篇文章的阅读,用户可以对Remoting的基本概念有了解,并知道几种Server端对象的区别和Server对象生命周期的概念。

  Remoting简介

  相信很多人都在不同的地方看到Remoting这个名词,其实它的概念很简单。我们通过使用Remoting来进行不同应用程序之间的通信,不管这些程序是在同一台计算机上,还是在局域网内的不同计算机上,甚至在Internet的不同操作系统上(当然相应的操作系统也必须实现了.net Framework)。Remoting的两大功能在于:

  1)不同进程间的通信。
  2)不同应用域(AppDomain),不同进程的对象之间的通信(可以使用多种通信协议)。

  .NET Remoting框架

  为了使用Remoting, .NET提供了一整套框架来使这种分布的调用透明化。它的框架如下图所示:



图1:.NET Remoting构架图

  通常用到的Remoting的概念有:

  Remoting Channel:这是指客户端和服务器端的通信协议,如我们可以使用TCP, HTTP协议。

  Serializer:这是指在传输时采用何种格式来传输数据,如我们可以采用Binary,也可以采用SOAP来传输XML格式的数据.

  .NET力图简化这些概念的编程,所以上面所述的协议和格式都可以通过更改配置文件切换。这也是编程人员不用担心的问题。如一段典型的客户端配置文件的内容是:

<CONFIGURATION>

<SYSTEM.RUNTIME.REMOTING>

 <APPLICATION>

<CHANNELS>

<CHANNEL ref="http" clientConnectionLimit="200">

 <CLIENTPROVIDERS>

<FORMATTER ref="binary">

 </CLIENTPROVIDERS>

</CHANNEL>

</CHANNELS>

</APPLICATION>

</SYSTEM.RUNTIME.REMOTING>

</CONFIGURATION>


Remoting的一个典型示例

  让我们首先来看一个典型的实例。我们会有一个服务器程序Server.exe和一个客户端程序CAOClient.exe。客户端程序会通过http channel调用服务器端RemoteType.dll的对象和方法。我们会来检查在几种不同的Server对象下,这些调用会有什么样不同的结果。

  服务器端代码


Server.cs
using System;
using System.Runtime.Remoting;

public class Server{
public static void Main(string[] Args){

// Load the configuration file
RemotingConfiguration.Configure("server.exe.config");

Console.WriteLine("The server is listening. Press Enter to exit....");
Console.ReadLine();

Console.WriteLine("GC'ing.");
GC.Collect();
GC.WaitForPendingFinalizers();

}
}

  表1:Server.cs源代码

Server.exe.config

<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<SERVICE>
<ACTIVATED type="ClientActivatedType, RemoteType">
</SERVICE>
<CHANNELS>
<CHANNEL ref="http" port="8088">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>

  表2:Server.exe.config源代码

RemoteType.cs
using System;
using System.Runtime.Remoting.Lifetime;
using System.Security.Principal;

public class ClientActivatedType : MarshalByRefObject{

private int i;
 // override the lease settings for this object
 public override Object InitializeLifetimeService(){
return null;
 }

 public string RemoteMethod(){

// announce to the server that we've been called.
Console.WriteLine("ClientActivatedType.RemoteMethod called.");

// report our client identity name

i=this.GetHashCode();
return "RemoteMethod called. " + i;
 }

 public string RemoteMethod1(){
return "RemoteMethod1 called. " + i;

}
}

  表3:RemoteType.cs源代码

  客户端代码


CAOClient.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;

public class Client{

public static void Main(string[] Args){

// Load the configuration file
RemotingConfiguration.Configure("CAOclient.exe.config");

ClientActivatedType CAObject = new ClientActivatedType();

Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod());
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod1());

Console.WriteLine("Press Enter to end the client application domain.");
Console.ReadLine();
}
}

  表4 CAOClient.cs源代码

CAOClient.exe.config
<CONFIGURATION>
<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<CLIENT url="http://localhost:8088">
<ACTIVATED type="ClientActivatedType, RemoteType">
</CLIENT>
<CHANNELS>
<CHANNEL ref="http" port="0">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>

  表5:CAOClient.exe.config源代码

  编译文件

  使用“Visual Studio .net Command Prompt="分别编译上述文件:

csc /target:library RemoteType.cs
csc Server.cs
csc –reference:RemoteType.dll CAOClient.cs
您会看到三个输出文件:RemoteType.dll, Server.exe 和 CAOClient.exe。
运行Remoting程序
在命令行方式下启动:Server.exe
在命令行方式下启动:CAOClient.exe

  程序分析

  这是一个非常简单的Remoting程序,如果去掉两个配置文件(Server.exe.config和CAOClient.exe.config),你简直看不到它和非Remoting的程序有什么差别。实际上,在两个配置文件中,我们只是配置了Remoting的Channel(http:8088)。
posted on 2006-07-04 19:36  骆驼SPACE  阅读(484)  评论(0)    收藏  举报