咱們已經知道能夠經過編碼的方式配置服務器通道和遠程客戶機,除此以外,還可使用配置文件對服務器通道和遠程客戶機進行配置。使用遠程客戶機和服務器對象的配置文件的優勢在於,用戶無需修改任何一行代碼,也無需進行從新編譯,即可以配置通道和遠程對象。html
.NET提供了Remoting配置文件的標準,基於XML格式。編程
一.配置文件服務器
1.服務器配置文件:app
先來看一個服務器配置文件的實例,而後我再具體解釋一下其中的內容:框架
<?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>下面。url
<application>元素使用name屬性指定了服務器的名稱,該應用程序提供了服務,並請求了服務的通道配置。spa
應用程序所提供的服務必須做爲<service>的子元素列出,這就是遠程對象自己,可使用<wellknown>元素來指定遠程對象,mode屬性能夠指定爲SingleCall或Singleton,在後面咱們會說到。同時用type屬性來指定已經定義了類型的對象,只須要指定程序集的名稱便可,不須要擴展名DLL。code
在<channels>元素中,咱們定義了服務器要使用的通道,用ref屬性能夠引用一個預先定義好的通道,同時必須使用port屬性爲通道分配端口,由於服務器必須有一個客戶機所熟知的端口號,以便客戶機能夠利用該端口號。這些通道在機器配置文件中已經定義預先定義了6個,咱們能夠打開Machine.config文件看一下,默認的路徑爲%SystemRoot%\Microsoft.NET\Framework\<vx.x.x>\CONFIG。orm
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.爲了防止配置文件找不到,咱們能夠在項目的屬性中設置,在生成後事件裏面填寫拷貝目錄語句:
如圖:
6.在編碼中,能夠不要把配置文件名硬編碼寫死,用以下語句來代替,這是一個很好的編程實踐,也是值得推薦的一種寫法。
7.最後一點,也是最重要的一點,推薦在項目中使用配置文件!
出處:http://www.cnblogs.com/Terrylee/archive/2005/11/17/278366.html