DotNet Remoting (二)

Using DotNet Remoting(一个例子)

 

There are three separate programs that must be created to build an application that uses remoting:

·         A remote class that allows clients to access and use the methods contained in the class.

·         A server program that advertises and hosts the server class on a machine

·         A client program that uses the server class methods

(一)创建一个Remote Class,完成两个整数加法的功能,注Remote Class必需继承自MarshalByRefObject。然后编译为dll库文件。此文件将会被Remote ServerClient用到。要保证服务端和客户端所用的Remote Class是相同的。

 

using System;

public class AddClass:MarshalByRefObject

{

  public int Add(int a, int b)

  {

   int c = a + b;

   return c;

  }

}

 

(二)创建Server ProgramThere are three steps to build the server program for the remote class:

1.    Create a communication channel to support access to the remote class.(创建连接通道)

2.    Register the communication channel with the remoting channel services.(注册连接通道)

3.    Register the remote class as a service with the remoting services. (注册远程类)

1Create a communication channel

DotNet提供了两个类TcpChannelHttpChannel来建立communication channelTcpChannelHttpChannel都通过绑定Tcp端口来监听请求,但是TcpChannel默认采用BinaryFormatter来序列化和传输数据,而HttpChannel默认采用SoapFormatter。用法如下:

TcpChannel chan = new TcpChannel(8004);

HttpChannel chan = new HttpChannel(8004);

 

2Register a communication channel

ChannelServices.RegisterChannel(chan);

 

3)Register the Remote Class Service

在创建和注册完连接通道后,需要把Remote Class注册为系统的服务,可以应用RemotingConfiguration类来进行注册。注册Remote Class Service需要3个参数

Parameter

Description

type

The full type name of the remote class object, along with the assembly filename where the class is defined

objectUri

The URI of the remote class object

mode

How the server will handle calls to the remote class object

Type参数用来指定Remote Class的类名和Assenbly文件名

Type=”AddClass, AddClass”;  //不需要指定扩展名

Mode参数有两种选择SingleCall或者Singleton

 

在确定完RemotingConfiguration参数后,需要在Server Program中来声明这些参数。有两种方法可以完成这项工作,一种是通过xml的配置文件,一种是写在程序里。(具体查看帮助)

 

下面是AddServer程序:

sing System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

public class AddServer

{

  public static int Main()

  {

   HttpChannel chan = new HttpChannel(8004);

   ChannelServices.RegisterChannel(chan);

   RemotingConfiguration.RegisterWellKnownServiceType(

     Type.GetType("AddClass, AddClass"), "MyAddServer",

     WellKnownObjectMode.SingleCall);

   Console.WriteLine("Hit <enter> to exit...");

   Console.ReadLine();

   return 0;

  }

}

 

注册完服务过后,Server Program必须要保持运行状态,才能监听远程请求。编译AddServer程序需要用到AddClass(The Remote Class)生成的dll库文件。如下:

csc /r:addclass.dll addserver.cs

 

(三)创建client program

同服务端程序一样,client program也需要创建连接通道,然后注册连接通道,只不过不需要指定端口号。如下:

HttpChannel chan = new HttpChannel();

ChannelServices.RegisterChannel(chan);

 

After the channel is created and registered, you can instantiate the proxy class to create an object of the remote class. This proxy class mimics the functionality of the remote class for local calls. When the proxy receives a call to a method for the remote class, it passes the call via the established communication channel to the server. The server receives the call and passes it to the remote class, which processes the call and passes any return information through the server program back to the proxy class.

There are two ways of creating the proxy object in the client program:

·         Registering the remote class as a client type in the remoting system and creating a new instance of the remote class for the proxy class

·         Using the Activator class to register the remote class information for the proxy class

下面是AddClient程序:

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

public class AddClient

{

  public static int Main (string[] argv)

  {

   HttpChannel chan = new HttpChannel();

   ChannelServices.RegisterChannel(chan);

   

    //下面的obj就叫做proxy object

   AddClass obj = (AddClass)Activator.GetObject(

     typeof(AddClass), "http://222.18.3.253:8004/MyAddServer");

   if (obj == null)

     System.Console.WriteLine("Could not locate server");

   else

   {

     int a = Convert.ToInt32(argv[0]);

     int b = Convert.ToInt32(argv[1]);

     int c = obj.Add(a, b);

     Console.WriteLine("a + b = {0}", c);

   }

   return 0;

  }

}

 

用如下命令编译:

csc /r:addclass.dll addclient.cs

 

(四)Creating a Proxy Class Using soapsuds

经过上面的三个过程A Remote Class, A Remote Server,  A Remote Client就做好了。但是在上面的过程中,我们既创建了服务端程序,又创建了客户端程序,我们知道Remoting Class的定义。在现实中多半不是这样,大多情况客户端不知道Remoting Class是如何定义的。DotNet提供工具,使客户端从Remote Server得到Remote Class的定义。这个工具就是soapsuds.exe。用它可以生成Remote Class的定义,或者是dll,或者源文件。然后在客户端调用Remote Class时候,除了创建和注册Communication Channel外,其余的部分就如同调用本地类一样。(具体使用查看帮助,参照比较wsdl.exe工具)

 

新的客户端程序就是如下的样子:(当然,编译的时候需要soapsuds.exe根据远程服务生成的proxy class源文件或库文件)

 

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

public class NewAddClient

{

  public static int Main(string[] argv)

  {

   HttpChannel chan = new HttpChannel();

   ChannelServices.RegisterChannel(chan);

   AddClass obj = new AddClass();

   if (obj == null)

     System.Console.WriteLine("Could not locate server");

   else

   {

     int a = Convert.ToInt32(argv[0]);

     int b = Convert.ToInt32(argv[1]);

     int c = obj.Add(a, b);

     Console.WriteLine("a + b = {0}", c);

   }

   return 0;

  }

}

posted on 2004-07-10 00:46  星星之火  阅读(1188)  评论(0)    收藏  举报

导航