天天看點

net Remoting(4)——配置檔案

RemotingConfiguration類進行類型注冊時,可以采用程式配置方式,也可以通過配置檔案來進行。這個類有一個Configure方法:

public static void Configure(

    string filename,

    bool ensureSecurity

)

Filename就就檔案名(配置檔案的檔案名),第二個參數用于選擇安全性的啟用與否

在服務端激活下用配置檔案來實作:

在控制台應用程式下添加SelfService.config檔案(一個檔案),添加以下内容:

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

<configuration>

  <system.runtime.remoting>

    <application name="ServerRemoting">

      <service>

        <wellknown mode="Singleton"

type="SelfRemote.selfRemoteObject,SelfRemote"

objectUri="selfRemoteObject"/>

      </service>

      <channels>

        <channel ref="http" port="10001"/>

      </channels>

    </application>

  </system.runtime.remoting>

</configuration>

在這個檔案中,通道注冊,類型注冊與程式中對應。然後把此檔案的 複制到輸出目錄 設定為 始終複制(為的就是在生成的控制台應用程式同級目錄,能夠找得到這個配置檔案)。

然後,在控制台可以:

Console.WriteLine("http 通道remoting服務開始……");

RemotingConfiguration.Configure("SelfService.config", false);

onsole.Read();

這樣就可以了。

在用戶端,如果采用用戶端激活方式,那麼同上差不多,同時也要保證在程式同級目錄能找到檔案(始終複制):

建立立ClientService.config檔案,檔案内容為:

        <channel ref="http"/>

這裡的channel不要再指定端口。

在測試中:

RemotingConfiguration.Configure("ClientService.config",false);

selfRemoteObject app1 = new selfRemoteObject();

Assert.AreEqual(0, app1.Unid);

部落格園大道至簡

<a href="http://www.cnblogs.com/jams742003/" target="_blank">http://www.cnblogs.com/jams742003/</a>

轉載請注明:部落格園

繼續閱讀