初識用.NET Remoting來開發分布式應用

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   sp   

一..NET Remoting簡介:

.NET Remoting從某種意義上講是DCOM的替代品。ASP.NET Web服務十分有用,但是這項技術在企業內連網的解決方案中,對於某些業務請求來說並不快,也沒有足夠的靈活性,而且,ASP.NET Web服務需要有運行時的支援。使用.NET Remoting技術後,可以將Web服務提供給世界上的任何地方。而且可以在所有的應用程式類型中運行Web服務。

二..NET Remoting 的基本原理:

體繫結構圖如下:

 

三.幾個重要的概念:

1.遠程對象:

遠程對象類是從MarshalByRefObject類中派生的。跨越應用程式定義域調用這個類需要使用代理。.NET Remoting支援兩種類型的遠程對象:知名的(Well-known)遠程對象和客戶啟用(Client-activated)遠程對象。遠程對象其實包括兩層含義:

操作遠程對象:對象運行在遠程,客戶段向他發送訊息;

傳遞遠程對象:將遠程對象拿到本地,或者將本機物件發送過去,對副本進行操作。

2.啟用:

使用new運算子可以啟用遠程對象。還有其它一些方式也可以啟用遠程對象,在以後的隨筆裡面我會介紹。

3.通道:

一個遠程對象使用通道發送和接收訊息。伺服器選擇一個通道來監聽請求,用戶端選擇通道來和伺服器通訊。Remoting提供了內建的通道:TCP通道和HTTP通道,我們也可以編寫自己的通道。

4.編組:

數組通過應用程式定義域被傳遞的過程稱為編組。將變數作為遠程對象的參數來發送時,這個變數必須被轉換,以便能夠通過應用程式定義域發送該變數。

5.監聽:

使用監聽,能夠將某些功能置入到方法調用鏈中。如果調用某個對象的方法,監聽層便能夠捕獲調用來轉換方法調用,或是完成某些日誌記錄。.NET Remoting調用鏈的每一部分都是用監聽。

四.開發Remoting三步走:

開發.NET Remoting分三步走,在這裡以一個簡單的例子來說明。

1.建立遠程對象:

繼承System.MarshalByRefObject

using System;using System.Collections;using System.Text;namespace SimpleRemoting{    public class HelloServer : MarshalByRefObject    {        public HelloServer()        {            ///輸出資訊,伺服器啟用            Console.WriteLine("伺服器啟用……");        }        public String HelloMethod(String name)        {            Console.WriteLine(                "伺服器端 : {0}", name);            return "這裡是:" + name;        }    }}

 

2.建立宿主應用程式:

註冊通道

註冊伺服器啟用的遠程對象

運行宿主程式

using System;using System.Net;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using System.Runtime.Remoting.Channels.Http;namespace SimpleRemoting {    public class Server    {        public static int Main(string [] args)         {                        ///建立Tcp通道            TcpChannel chan1 = new TcpChannel(8085);            ///建立Http通道            HttpChannel chan2 = new HttpChannel(8086);                        ///註冊通道            ChannelServices.RegisterChannel(chan1);            ChannelServices.RegisterChannel(chan2);            RemotingConfiguration.RegisterWellKnownServiceType                (                typeof(HelloServer),                "SayHello",                WellKnownObjectMode.Singleton                );                        System.Console.WriteLine("按任意鍵退出!");            ///下面這行不能少            System.Console.ReadLine();            return 0;        }    }}

 

3.建立用戶端程式:

註冊通道

根據URL得到對象代理

使用代理調用遠程對象

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using System.Runtime.Remoting.Channels.Http;using System.IO;namespace SimpleRemoting {    public class Client    {        public static void Main(string[] args)        {            ///使用TCP通道得到遠程對象            TcpChannel chan1 = new TcpChannel();            ChannelServices.RegisterChannel(chan1);            HelloServer obj1 = (HelloServer)Activator.GetObject(                typeof(SimpleRemoting.HelloServer),                "tcp://localhost:8085/SayHello");            if (obj1 == null)            {                System.Console.WriteLine(                    "串連TCP伺服器失敗");            }            ///使用HTTP通道得到遠程對象            HttpChannel chan2 = new HttpChannel();            ChannelServices.RegisterChannel(chan2);            HelloServer obj2 = (HelloServer)Activator.GetObject(                typeof(SimpleRemoting.HelloServer),                "http://localhost:8086/SayHello");            if (obj2 == null)            {                System.Console.WriteLine(                    "串連HTTP伺服器失敗");            }                        ///輸出資訊            Console.WriteLine(                "ClientTCP HelloMethod {0}",                obj1.HelloMethod("Caveman1"));            Console.WriteLine(                "ClientHTTP HelloMethod {0}",                obj2.HelloMethod("Caveman2"));            Console.ReadLine();        }    }}

 

結束語:初識用.NET Remoting來開發分布式應用就到這裡了,有時間我會就.NET Remoting技術寫成系列文章。包括基於租約的生存期,編組,非同步遠程調用等等。

出處:http://www.cnblogs.com/Terrylee/archive/2005/11/03/267621.html

初識用.NET Remoting來開發分布式應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.