Remoting 的“传递的引用”理解

  WCf是集大成者,具有其他微软的很多技术,其中分布式上很多借助于Remoting,所以研究一下Remoting有助于理解WCF

  提到Remoting就不得不涉及到MarshalByRefObject这个对象,网络上对这个词语的解释非常的模糊不清,特别是说“跨域访问,Remoting是引用传递,不是值传递”,而没有讲“引用传递”到底是传的什么东西,有的地方确实说了通过ObjRef对象 传递需要交互的所有内容信息,但是更多的是对“引用传递”没有清楚的认识,或者说是通过其他方式来理解“引用传递”而不是通过“引用传递”来更好的理解Remoting,这种逻辑思维方式,特别容易产生囫囵吞枣的模糊概念,看似明白,其实不明白。

  为了理解引用传递,写了一个例子,可能有助于理解 代码

定义Interface

public interface IPersonService
    {
        String HelloMethod(String name);
    }

定义 Implement

public class PersonService : MarshalByRefObject, IPersonService
{
    public String HelloMethod(String name)
    {
        Console.WriteLine(
            "Server Hello.HelloMethod : {0}", name);
        return "Hi there " + name;
    }
}

定义Service
static void Main(string[] args)
        {
            //TCP协议传输消息的信道实现
            TcpChannel chan1 = new TcpChannel(8085);
            //为远程调用实现使用HTTP协议传输消息的客户端通道
            HttpChannel chan2 = new HttpChannel(8086);
            //提供帮助进行远程处理信道注册、解析和URL发现的静态方法。无法继承此类
            ChannelServices.RegisterChannel(chan1, false);
            ChannelServices.RegisterChannel(chan2, false);
            //提供多种配置远程结构的静态方法
            RemotingConfiguration.RegisterWellKnownServiceType
                (
               //typeof(HelloServer),
               typeof(PersonService),
                "SayHello",
                WellKnownObjectMode.Singleton
                );


            System.Console.WriteLine("Press Enter key to exit");
            System.Console.ReadLine();
        }
定义Client
class Program
    {
        static void Main(string[] args)
        {
            TcpChannel chan1 = new TcpChannel();
            ChannelServices.RegisterChannel(chan1,false);
            //Activator包含特定的方法,用以在本地或从远程创建对象类型、或获取对现有远程对象的引用。无法继承此类
            IPersonService obj1 = (IPersonService)Activator.GetObject(
                typeof(IPersonService),// typeof(HelloServer),
                "tcp://localhost:8085/SayHello");
            if (obj1 == null)
            {
                System.Console.WriteLine(
                    "Could not locate TCP server");
            }
            //使用HTTP通道得到远程对象
            HttpChannel chan2 = new HttpChannel();
            ChannelServices.RegisterChannel(chan2,false);
            IPersonService obj2 = (IPersonService)Activator.GetObject(
                typeof(IPersonService),// typeof(HelloServer),
                "http://localhost:8086/SayHello");
            if (obj2 == null)
            {
                System.Console.WriteLine(
                    "Could not locate HTTP server");
            }

            Console.WriteLine(
                "Client1 TCP HelloMethod {0}",
                obj1.HelloMethod("Caveman1"));
            Console.WriteLine(
                "Client2 HTTP HelloMethod {0}",
                obj2.HelloMethod("Caveman2"));
            Console.ReadLine();
        }
    }

注意,客户端使用的 “IPersonService”接口,对于客户端来讲是不知道接口的实例类的,

客户端方法的调用也只是通过Activator生成的代理对象把调用的信息、链接信息等等进行打包,

服务端接收到打包过来的信息后,根据打包信息内容,对相应的类和方法进行操作,比如:创建(调用)服务实体,对方法进行调用,把返回值进行打包并传输给客户端端,里边具体的逻辑还需要继续研究

这里只想说明一点,“Remoting传输的引用”,到底是什么意思,免得误解。

原文地址:https://www.cnblogs.com/beixing/p/3848324.html