Effective C#: 4.使用类厂(Class Factory)模式实现基于接口的客户激活远程对象(上)

    技术2022-05-11  53

    Effective C#: 4.使用类厂(Class Factory)模式实现

    基于接口的客户激活远程对象(下)

     陈铭        Microsoft C#/.NET Asia MVP

     难度:7/10                       

    简便的分布式应用程序开发无疑是.NET平台最引人注目的部分。通过使用.NET Remoting技术,我们可以轻松的跨越运行环境(Context)、线程抑或进程的边界,甚至透过Internet访问远在他乡的另一台计算机上的某个对象。而且,这种远程访问是近乎透明的——在完成远程对象的一些初始化工作之后,对其方法的调用与普通本地对象几乎完全相同。

     

    根据生命周期控制方法的不同,.NET Remoting将远程对象分为服务器激活对象(SAO, Server Activated Object)客户激活对象(CAO, Client Activated Object)两种。顾名思义,服务器掌握着SAO对象生杀予夺的大权——更具体地说,将由服务器控制实际生成的远程对象的数量以及每个客户请求究竟由哪个远程对象处理;对于CAO对象,服务器会根据客户的请求建独立的远程对象,每一个方法调用都会被指派到与这个客户相关联的远程对象上。CAO对象的生存周期则是由客户端通过定期更新它与服务器签订的远程对象的“租用协议”来控制的。

    让我们先来看一下SAO的情形。以下是一个简单的.NET Remoting SAO对象应用的完整程序 

    //share.cs, Remote Object

    namespace Effective.Csharp.Chapter4 {

        //Must inherit from MarshalByRefObject

        public class RemoteObject : System.MarshalByRefObject {

             //a very simple method implementation

            public String SayHello(String name) {

                return "Hello, " + name;

            }

        }

    }

     

    //server.cs, Server side code

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Tcp;

     

    namespace Effective.Csharp.Chapter4 {

        public class Server {

            public static int Main() {

                //Register the channel

                TcpChannel chan = new TcpChannel(8085);

                ChannelServices.RegisterChannel(chan);

     

                //Register the remote object

                RemotingConfiguration.RegisterWellKnownServiceType(

    Type.GetType("Effective.Csharp.Chapter4.RemoteObject, share"),

    "Hello.rem", WellKnownObjectMode.SingleCall);

                  

                   //Hold the server, wait for client

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

                System.Console.ReadLine();

                return 0;

            }

        }

    }

     

    //client.cs, Client Side code

    using System;

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Tcp;

     

    namespace Effective.Csharp.Chapter4 {

      public class Client

      {

        public static int Main()

        {

                TcpChannel chan = new TcpChannel();

          ChannelServices.RegisterChannel(chan);

     

          //Create the remote object

    RemoteObject obj =

    (RemoteObject)Activator.GetObject(

    typeof(RemoteObject), "tcp://localhost:8085/Hello.rem");

            

    Console.WriteLine(obj.SayHello("World"));

                return 0;

        }

      }

    }

     

    整个应用实际上被分解成三个独立编译的部分:share.cs包含了远程对象RemoteObject的定义,server.cs是创建和维护RemoteObject的服务器端代码,而client.cs则是实际应用RemoteObject的客户。一个特别需要注意的问题是:client.cs代码多次引用了RemoteObject类型(恰恰相反,server.cs没有直接引用RemoteObject)。在条款1中我们曾经提到如果使用了在某个类集中定义的类型,就必须在编译期间使用/r选项引用相关的类集。换言之,在编译client.cs的时候必须显式引用share类集,而编译之后的client应用程序在缺少了share类集的情况下根本无法正确运行:

       csc server.cs

    csc /t:library share.cs

    csc /r:share.dll client.cs

     

    但是,在真实的分布式应用当中,绝不可能把远程对象的实现代码和客户端程序一起发布——尤其是当这些对象用于实现一些至关重要的算法(例如加密、用户验证等)的时候!

     

    事实上客户端的编译和运行并不真的需要远程对象的实现——客户端只是负责将对象方法的调用转换成消息的形式,然后发送给服务器做进一步处理。之所以需要引用包含远程对象实现的类集,只是因为编译器需要验证这些方法调用代码的参数和返回值类型正确无误,而运行时需要利用这些信息生成将方法调用转换成消息的代理。也就是说,编译客户端程序真正需要的只是远程对象的类型元数据(MetaData),而不是实现!

     

     

    由此想到的一个显而易见的解决方法是使用接口和抽象基类——它们恰好是用于将具体的类实现与类方法定义分离开来。下面给出了利用接口继承实现上述远程对象应用实例的程序代码:

    //share.cs, Remote Object Interface definition

    namespace Effective.Csharp.Chapter4 {

        public interface IRemoteObject {

            String SayHello(String name);

        }

    }

     

    //server.cs, Server side code

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Tcp;

     

    namespace Effective.Csharp.Chapter4 {

        //Must inherit from MarshalByRefObject

     public class RemoteObject : System.MarshalByRefObject,

                                 IRemoteObject {

             //a very simple method implementation

            public String SayHello(String name) {

                return "Hello, " + name;

            }

        }

     

        public class Server {

            public static int Main() {

                //Register the channel

                TcpChannel chan = new TcpChannel(8085);

                ChannelServices.RegisterChannel(chan);

     

                //Register the remote object

                RemotingConfiguration.RegisterWellKnownServiceType(

    Type.GetType("Effective.Csharp.Chapter4.RemoteObject, server"),

    "Hello.rem", WellKnownObjectMode.SingleCall);

                  

                   //Hold the server, wait for client

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

                System.Console.ReadLine();

                return 0;

            }

        }

    }

     

    //client.cs, Client Side code

    using System;

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Tcp;

     

    namespace Effective.Csharp.Chapter4 {

      public class Client

      {

        public static int Main()

        {

                TcpChannel chan = new TcpChannel();

          ChannelServices.RegisterChannel(chan);

     

          //Create the remote object

    IRemoteObject obj =

    (IRemoteObject)Activator.GetObject(

    typeof(IRemoteObject), "tcp://localhost:8085/Hello.rem");

            

    Console.WriteLine(obj.SayHello("World"));

                return 0;

        }

      }

    }

     

    整个应用仍然被分成三个部分,但这一次share.cs不再包含远程对象的实现,而仅仅是用于描述远程对象方法的一个接口。远程对象实际的实现被转移到了server.cs当中。这样,在client.csserver.cs的编译过程中只需要引用share.cs中定义的接口:

      csc /t:library share.cs

       csc /r:share.dll client.cs

       csc /r:share.dll server.cs

    实际发布的客户端程序只需要包含client.exeshare.dll,不再包括远程对象方法实现的代码了。

     

    除了使用接口以外,使用.NET框架提供的SoapSuds.exe工具同样可以完成上述工作。SoapSuds可以从一个已有的类集中分离出类型定义的元数据信息,分离出的部分可以用于客户端代码的编译和发布:

    //Extract remote object metadata from share.dll

    SOAPSUDS -types: Effective.Csharp.Chapter4.RemoteObject,share

    -oa:RemoteObject.dll

       //compile the client using extracted dll

       csc /r:RemoteObject.dll client.cs

    与使用接口的解决方法相比,使用SoapSuds工具要简单的多,但是使用接口继承可以使你的远程对象具备多态特性——同样的客户端不需要任何改动就可以用于任何一个实现了特定接口的远程对象。而且,使用接口的解决方案要更清晰一些——两个同名的RemoteObject类多少会令程序员感到有些混淆。(未完待续)


    * 本文系原创作品,未经作者本人许可请勿转载。


    最新回复(0)