使用.NET Remoting開發分布式應用——設定檔篇

來源:互聯網
上載者:User

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

我們已經知道可以通過編碼的方式設定管理員通道和遠程客戶機,除此之外,還可以使用設定檔對伺服器通道和遠程客戶機進行配置。使用遠程客戶機和伺服器對象的設定檔的優點在於,使用者無需修改任何一行代碼,也無需進行重新編譯,便可以配置通道和遠程對象。

.NET提供了Remoting設定檔的標準,基於XML格式。

 

一.設定檔

1.伺服器設定檔:

先來看一個伺服器設定檔的執行個體,然後我再具體解釋一下其中的內容:

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.runtime.remoting>        <application>            <service>                <wellknown                     mode="Singleton"                     type="RemotingConfigDemo.HelloServer, General"                     objectUri="SayHello" />            </service>            <channels>                <channel port="8086" ref="http"/>            </channels>        </application>    </system.runtime.remoting></configuration>

 

在伺服器設定檔中,最外層的元素是<configuration>,這是所有設定檔的共性(包括Web.config設定檔)。

所有的遠程配置項必須作為子項目添加到<system.runtime.remoting>下面。

<application>元素使用name屬性指定了伺服器的名稱,該應用程式提供了服務,並請求了服務的通道配置。

應用程式所提供的服務必須作為<service>的子項目列出,這就是遠程對象本身,可以使用<wellknown>元素來指定遠程對象,mode屬性可以指定為SingleCall或Singleton,在後面我們會說到。同時用type屬性來指定已經定義了類型的對象,只需要指定程式集的名稱即可,不需要副檔名DLL。

在<channels>元素中,我們定義了伺服器要使用的通道,用ref屬性可以引用一個預先定義好的通道,同時必須使用port屬性為通道分配連接埠,因為伺服器必須有一個客戶機所熟知的連接埠號碼,以便客戶機可以利用該連接埠號碼。這些通道在機器設定檔中已經定義預先定義了6個,我們可以開啟Machine.config檔案看一下,預設的路徑為%SystemRoot%\Microsoft.NET\Framework\<vx.x.x>\CONFIG。

2.客戶機設定檔:

典型的客戶機設定檔如下所示:

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.runtime.remoting>        <application>            <client>                <wellknown type="RemotingConfigDemo.HelloServer, General" url="http://localhost:8086/SayHello" />            </client>            <channels>                <channel ref="http" port="0"></channel>            </channels>        </application>    </system.runtime.remoting></configuration>

 

同伺服器設定檔的元素一樣,不同的是這次是客戶機通道,所以它不需要指定連接埠號碼,我們可以暫時指定為0號。其他的保持不變。

 

二.樣本程式

1.遠程對象代碼:

using System;using System.Text;using System.Runtime.Remoting.Lifetime;namespace RemotingConfigDemo{    public class HelloServer : MarshalByRefObject    {        public HelloServer()        {            Console.WriteLine("伺服器啟用……");        }        public String HelloMethod(String name)        {            Console.WriteLine(                "伺服器端 :{0}", name);            return "這裡是:" + name;        }     }}

 

2.伺服器

設定檔:

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.runtime.remoting>        <application>            <service>                <wellknown                     mode="Singleton"                     type="RemotingConfigDemo.HelloServer, General"                     objectUri="SayHello" />            </service>            <channels>                <channel port="8086" ref="http"/>            </channels>        </application>    </system.runtime.remoting></configuration>

 

伺服器代碼:

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using System.Runtime.Remoting.Channels.Http;namespace RemotingConfigDemo {    public class Server    {        public static int Main(string [] args)         {            RemotingConfiguration.Configure("Server.exe.config");            System.Console.WriteLine("按任意鍵退出……");            System.Console.ReadLine();            return 0;        }    }}

 

3.客戶機

設定檔:

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.runtime.remoting>        <application>            <client>                <wellknown type="RemotingConfigDemo.HelloServer, General" url="http://localhost:8086/SayHello" />            </client>            <channels>                <channel ref="http" port="0"></channel>            </channels>        </application>    </system.runtime.remoting></configuration>

 

客戶機代碼:

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 RemotingConfigDemo {    public class Client    {        public static void Main(string[] args)        {            //使用HTTP通道得到遠程對象            RemotingConfiguration.Configure("Client.exe.config");            HelloServer obj2 = new HelloServer();            if (obj2 == null)            {                System.Console.WriteLine(                    "串連HTTP伺服器失敗……");            }            Console.WriteLine(                "Client2 HTTP HelloMethod {0}",                obj2.HelloMethod("Caveman2"));            Console.ReadLine();        }    }}

 

 

三.需要注意的幾點

1.程式集的名稱常常會和儲存程式集的檔案的名稱相混淆。程式集的名稱是HelloServer,而組件檔的名稱是HelloServer.dll。使用方法調用時,需要將程式集的名稱作為參數,而不需要使用檔案的副檔名。

2.必須將遠程對象類的程式集複製到服務程式的可執行檔的目錄中,或是通過添加DLL引用。因為通過讀取設定檔,將執行個體化遠程架構中的這個遠程對象類,程式集必須位於能夠被找到的位置。

3.一般來說,我們可以讓應用程式的設定檔名和可執行檔的檔案名稱相同,其後跟有副檔名.config。

4.如果用App.config作為伺服器或客戶機設定檔,要注意App.config檔案在運行後自動變為[應用程式名稱].exe.config。

5.為了防止設定檔找不到,我們可以在項目的屬性中設定,在產生後事件裡面填寫拷貝目錄語句:

 

copy "$(ProjectDir)\*.config" "$(TargetDir)"

 

 



6.在編碼中,可以不要把設定檔名寫入程式碼寫死,用如下語句來代替,這是一個很好的編程實踐,也是值得推薦的一種寫法。

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

7.最後一點,也是最重要的一點,推薦在項目中使用設定檔!

出處:http://www.cnblogs.com/Terrylee/archive/2005/11/17/278366.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.