[Remoting] 二:遠程對象

來源:互聯網
上載者:User

[Remoting] 二:遠程對象

在分布系統中,遠程對象需要跨越應用程式定義域進行傳遞,因此其表示方式會有所不同。基於效能和資料共用等原因考慮,Remoting 中遠程對象可以是 "值封送對象(MBV)" 或 "引用封送對象(MBR)"。

MBV 機制類似於 Web 無狀態請求,伺服器建立對象執行個體傳遞給通道發送到用戶端,而後伺服器端不再繼續維護其狀態和生存期。而 MBR 則在其生存期內一直存活在伺服器程式域中,用戶端只是通過代理對象來完成調用訊息傳遞,用戶端可以通過相關介面來延長遠程對象的生存期。

實現 MBV 一般通過 SerializableAttribute 特性,或者實現 ISerializable 介面。運行下面的例子,我們會發現遠程對象在用戶端程式域內,並且不是代理對象。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;

namespace Learn.Library.Remoting
{
  public class RemotingTest2
  {
    /// <summary>
    /// 遠程類型
    /// </summary>
    [Serializable]
    public class Data
    {
      private int i;

      public int I
      {
        get { return i; }
        set { i = value; }
      }

      public void Where()
      {
        Console.WriteLine("{0} in {1}", this.GetType().Name, AppDomain.CurrentDomain.FriendlyName);
      }
    }

    /// <summary>
    /// 伺服器端代碼
    /// </summary>
    static void Server()
    {
      // 建立新的應用程式定義域,以便類比分布結構。
      AppDomain server = AppDomain.CreateDomain("server");
      server.DoCallBack(delegate
      {
        // 建立並註冊通道
        TcpServerChannel channel = new TcpServerChannel(801);
        ChannelServices.RegisterChannel(channel, false);

        // 註冊遠程類型
        RemotingConfiguration.ApplicationName = "test";
        RemotingConfiguration.RegisterActivatedServiceType(typeof(Data));
      });
    }

    /// <summary>
    /// 用戶端代碼
    /// </summary>
    static void Client()
    {
      // 建立並註冊通道
      TcpClientChannel channel = new TcpClientChannel();
      ChannelServices.RegisterChannel(channel, false);

      // 註冊遠程類型
      RemotingConfiguration.RegisterActivatedClientType(typeof(Data), "tcp://localhost:801/test");

      // 建立遠程對象並調用其方法
      Data data = Activator.CreateInstance(typeof(Data)) as Data;
      data.Where();

      // 判斷是否是代理
      Console.WriteLine(RemotingServices.IsTransparentProxy(data));
    }

    static void Main()
    {
      Server();
      Client();
    }
  }
}


輸出:
Data in Learn.CUI.vshost.exe
False

MBR 則要求繼承自 MarshalByRefObject 或 ContextBoundObject。繼承自 ContextBoundObject 的遠程對象,可以包含其執行內容,比如事務等等,其效能不如 MarshalByRefObject。下面的例子中,遠程對象依舊在伺服器程式域內執行,用戶端只是一個代理對象在轉寄調用。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;

namespace Learn.Library.Remoting
{
  public class RemotingTest2
  {
    /// <summary>
    /// 遠程類型
    /// </summary>
    public class Data : MarshalByRefObject
    {
      private int i;

      public int I
      {
        get { return i; }
        set { i = value; }
      }

      public void Where()
      {
        Console.WriteLine("{0} in {1}", this.GetType().Name, AppDomain.CurrentDomain.FriendlyName);
      }
    }

    /// <summary>
    /// 伺服器端代碼
    /// </summary>
    static void Server()
    {
      // 建立新的應用程式定義域,以便類比分布結構。
      AppDomain server = AppDomain.CreateDomain("server");
      server.DoCallBack(delegate
      {
        // 建立並註冊通道
        TcpServerChannel channel = new TcpServerChannel(801);
        ChannelServices.RegisterChannel(channel, false);

        // 註冊遠程類型
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data",
          WellKnownObjectMode.Singleton);
      });
    }

    /// <summary>
    /// 用戶端代碼
    /// </summary>
    static void Client()
    {
      // 建立並註冊通道
      TcpClientChannel channel = new TcpClientChannel();
      ChannelServices.RegisterChannel(channel, false);

      // 建立遠程對象並調用其方法
      Data data = (Data)Activator.GetObject(typeof(Data), "tcp://localhost:801/data");
      data.Where();

      // 判斷是否是代理
      Console.WriteLine(RemotingServices.IsTransparentProxy(data));
    }

    static void Main()
    {
      Server();
      Client();
    }
  }
}


輸出:
Data in server
True

MBV 傳遞完整的副本到用戶端,而後的所有調用都是在用戶端程式域內進行,不再需要跨域的往返過程。但完整複製和序列化需要更多的資源和往返時間,因此不適合較大的對象,同時無法在多個用戶端間共用資訊。而 MBR 的所有調用都是通過代理進行的,因此往返過程比較多,但因執行過程都在伺服器端進行,其整體效能還是非常高的。在系統設計時可以針對不同的應用採取不同的選擇。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.