Castle整合.NETRemoting

  今天研究了一下Castle的Remoting Facility.記錄如下:

十載的云陽網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整云陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“云陽網(wǎng)站設(shè)計”,“云陽網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

微軟以前使用COM/DCOM的技術(shù)來處理分布式系統(tǒng)架構(gòu),通過Client端的Proxy代理程序來呼叫遠(yuǎn)程Server機器上的對象。.NET Framework則使用.NET Remoting或Web Services技術(shù)來實作分布式處理的工作概念;在這里針對.NET Remoting的設(shè)計架構(gòu)做一個初步的簡介和Castle整合示例。

.NET Framework提供了多種的機制來支持Remoting,如:

.利用Channel來負(fù)責(zé)信息的發(fā)送與接收。
.利用Formatter來負(fù)責(zé)在信息要通過channel發(fā)送出去之前,先將信息做適當(dāng)?shù)募用埽蛴谛畔⒃谕ㄟ^Channel接收進(jìn)來之后,先將信息做相對的解密工作。
.利用Proxy來呼叫遠(yuǎn)程的對象執(zhí)行所要的功能呼叫。

其關(guān)系如下圖所示:

Castle 整合.NET Remoting
Channel 和 Formatter

在遠(yuǎn)程對象被使用之前,必須先在Server端注冊好信息發(fā)送的信道(Channel),這些Channel可通過.NET Remotin configuration file或 ChannelServices對象類別的RegisterChannel方法來注冊。

在Channel的使用上,.NET Framework支持HTTP、TCP及SMTP等通道。若使用HTTP Channel ,則使用SOAP協(xié)議來收送信息,所有的信息會被發(fā)送到SOAP Formatter中,被序列化(serialized)成XML的格式,而SOAP所需的headers也會被加入。至于使用TCP Channel者,則使用TCP協(xié)議來將信息發(fā)送到Binary Formatter中,以Binary Stream的方式來將信息發(fā)送到URI目的地。(URI : Universal Resource Identifier,類似大家所熟悉的URL)。

Activation and Proxy
Server-Side Activation
Server端在Client端要獲取Remoting對象時必需在Server端能自動啟動Remoting對象,可使用RemotingConfiguration對象類別的RegisterWellKnownServiceType方法來完成這項工作。

Client-Side Activation
Client端要使用遠(yuǎn)程對象之前,可使用New 或Activator 對象類別所提供的CreateInstance或GetObject方法來啟動對象并傳回Proxy,以便Client端可通過Proxy來執(zhí)行叫用遠(yuǎn)程對象的方法。

范例
以下分三個步驟來介紹

1.    建立Remoting對象

2.    在Server上初始Remoting物件

3.    Client端使用Remoting對象

步驟1:建立Remoting對象
建立一個MathServer對象類別,提供Sum方法,可給予一連串的整數(shù)由Sum方法代為計算總和。程序代碼如下,并說明于后:

using System;

 

namespace RemoteSample.Components

{

     /// <summary>

     /// Class1 的摘要說明。

     /// </summary>

     public interface IRemoteMath

     {

         int Sum(params int[] a);

 

         int CallCounter

         {

              get;

         }

     }

}

 

using System;

using RemoteSample.Components;

 

namespace RemoteSample.Components

{

     /// <summary>

     /// RemoteMath 的摘要說明。

     /// </summary>

     public class RemoteMath: MarshalByRefObject,IRemoteMath

     {

         private int callCounter = 0;

 

         public RemoteMath()

         {

             

         }

 

 

         #region 接口IRemoteMath的成員實現(xiàn)

         /// <summary>

         /// 求和計算

         /// </summary>

         /// <param name="a"></param>

         /// <returns></returns>

         public int Sum(params int[] a)

         {

              int sum = 0;

              for (int i = 0; i <= a.Length - 1; i++)

              {

                   sum += a[i];

              }

              callCounter += 1;

              return sum;

         }

 

        

         public int CallCounter

         {

              get

              {

                   return this.callCounter;

              }

         }

    

         #endregion

     }

}

 

說明:Remoting對象必須繼承自MarshalByRefObject,這樣才能通過網(wǎng)絡(luò),將對象執(zhí)行個體的參考位置傳遞給呼叫端。

步驟2:在Server上初始化Remoting對象,程序代碼如下,并說明于后:

namespace RemoteSample.Server

{

 

     class RemoteServerMain

     {

         [STAThread]

         internal static void Main(string[] args)

         {

              IWindsorContainer container = new RemotingContainer();

    

              Console.ReadLine();

         }

     }

}

ServerConfig.xml文件:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 

     <facilities>

         <facility id="remote.facility" type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting"

                   remotingConfigurationFile ="../../RemotingTcpConfig.config"

                isServer="true"

                 registryUri="kernel.rem" >

         </facility>

     </facilities>

    

     <components>

         <component

              id="remote.math"

              service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"

              type="RemoteSample.Components.RemoteMath, RemoteSample.Components"

              remoteserver="component"  >

         </component>

     </components>

 

</configuration>

RemotingTcpConfig.config文件:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.runtime.remoting>

       <application>

           <channels>

              <channel ref="tcp" port="2133" />

           </channels>

       </application>

    </system.runtime.remoting>

</configuration>

 

說明:

使用Castle 的Remoting Facillity 使用Remoting 。

1.配置指出在2133 port上要建立TCP Channel, 2133 port上要建立tcp Channel

 

2.<components>指出在Server端注冊所要使用的組件、服務(wù)的名稱及啟動的方式。其中component表示一個執(zhí)行個體可供多個前端來呼叫,可保留其狀態(tài),另一種則為ClientActivated,一個執(zhí)行個體只能服務(wù)一個前端的呼叫,無法保留其狀態(tài)。

步驟3:在Client端使用Remoting對象

ClientConfig.xml

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 

     <facilities>

         <facility

                   id="remote.facility"

                 type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting"

                 remotingConfigurationFile="../../RemotingTcpConfigClient.config"

                 isClient="true"

                 remoteKernelUri="tcp://localhost:2133/kernel.rem"

                 baseUri="tcp://localhost:2133" >

         </facility>

     </facilities>

 

  <components>

 

    <component

         id="remote.math"

         service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"

         type="RemoteSample.Components.RemoteMath, RemoteSample.Components"

         remoteclient="component" />

 

  </components>

 

</configuration>

RemotingTcpConfigClient.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

     <system.runtime.remoting>

         <application>

              <channels>

                   <channel ref="tcp" port="0" />

              </channels>

         </application>

     </system.runtime.remoting>

</configuration>

程序代碼如下:

namespace RemoteSample.Client

{

     /// <summary>

     /// RemoteClient的摘要說明。

     /// </summary>

     public class RemoteClientMain

     {

         [STAThread]

         static void Main(String[] args)

         {

              IWindsorContainer container = new RemotingContainer();

              IRemoteMath remoteMath = (IRemoteMath)container[typeof(IRemoteMath)] ;

              Console.WriteLine("Client1 TCP Call Sum method {0} Counter {1}",remoteMath.Sum(10, 20, 30),remoteMath.CallCounter);

 

              Console.WriteLine("....press a key to stop");

              Console.ReadLine();

         }

     }

}

新航道雅思

網(wǎng)頁標(biāo)題:Castle整合.NETRemoting
標(biāo)題URL:http://muchs.cn/article10/iiojdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航電子商務(wù)、云服務(wù)器、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名