今天學習了伺服器端激活和用戶端激活的差別!可還是出現了一點點的差錯,經過對比得到正确的調用方法,整理如下:
1.伺服器端激活,分為兩種方式Singleton和SingleCall方式
Server端App.config設定,在這裡wellknown指向了類庫RemoteObject的Myobject類,設定的Uri位址為RemoteObject.MyObject;采用的模式是Singleton模式。
且通道協定是Tcp協定,端口号為9999。
伺服器端app.config
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject"
mode="Singleton" />
</service>
<channels>
<channel ref="tcp" port="9999"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client端App.config設定,在這裡設定了連接配接關鍵字ServiceURL,這個連接配接關鍵字的值 tcp://localhost:9999/RemoteObject.MyObject,也就是采用tcp協定通路本地的9999端口号的Uri為 RemoteObject.MyObject的伺服器端。
<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
</appSettings>
用戶端調用RemoteObject.MyObject類的實作代碼如下,Activator.GetObject方法建立目前運作的遠端對象、由伺服器激活的已知對象或 XML Web services 的代理。您可以指定連接配接媒體(即通道)。
代碼:
RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),
System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
RemoteObject.MBV mbv = app.GetMBV();
2.用戶端激活。在用戶端請求的時候就激活了對象。
Server端App.config設定如下,設定
代碼
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<activated type="RemoteObject.MyObject,RemoteObject"/>
</service>
<channels>
<channel ref="tcp" port="9999"/>
</channels>
</application>
</system.runtime.remoting>
Client端App.config設定如下,在這裡Value值為tcp://localhost:9999/RemoteServer,這裡的Uri指向的是Server端App.config檔案中Application節的名字。
<add key="ServiceURL" value="tcp://localhost:9999/RemoteServer"/>
Client調用遠端對象代碼如下,在這裡使用的是Activator.CreateInstance方法,該通過調用與指定參數比對程度最高的構造函數來建立在程式集中定義的類型的執行個體。如果沒有指定任何參數,則将調用不帶任何參數的構造函數(即預設構造函數)。
本文轉自程興亮 51CTO部落格,原文連結:http://blog.51cto.com/chengxingliang/821189