国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

構建基本的.NET Remoting應用程序

2024-07-10 12:58:45
字體:
來源:轉載
供稿:網友
構建一個使用.net遠程處理框架來進行應用域(application domain )間通信的應用程序很簡單。你必須實現遠程類型(remotable type)、用于監聽請求的服務應用域和客戶應用域。同時,你必須為每個應用域配置遠程處理系統(remoting system ),以便可以使用遠程激活( remote activation )來激活遠程類型。
一、創建遠程類型(remotable type):
為了使其它應用域中的對象可以使用你的類實例,你的類必須從system.marshalbyrefobject類進行派生。下面的代碼說明如何創建遠程類:
[visual basic]
' remotabletype.vb
imports system

public class remotabletype
inherits marshalbyrefobject
private _internalstring as string = "this is the remotabletype."

public function stringmethod() as string
return _internalstring
end function 'stringmethod
end class 'remotabletype
[c#]
// remotabletype.cs
using system;
public class remotabletype : marshalbyrefobject{
private string _internalstring = "this is the remotabletype.";
public string stringmethod(){
return _internalstring;
}
}
為了使用.net framework sdk附帶的命令行工具來將上例中的類編譯成為庫文件,只要將它保存為remotabletype.language-extension(此處language-extension是你所使用的語言,比如cs、vb)。編譯時使用如下命令:
[visual basic]
vbc /t:library remotabletype.vb

[c#]
csc /noconfig /t:library remotabletype.cs

二、創建服務應用(host application):
要在不同地應用域(application domain)中創建遠程對象的實例,你必須創建服務應用來完成兩個任務:
(1)選擇并且注冊一個通道(channel)。該通道是一個處理網絡協議和串行格式化(serialization format)的對象。
(2)在.net遠程系統(.net remoting system)中注冊你創建的遠程類型,這樣遠程系統就能使用你的通道來監聽對于你遠程類型的各種請求。
.net框架有兩個默認的通道:system.runtime.remoting.channels.http.httpchannel(使用soap格式)和 system.runtime.remoting.channels.tcp.tcpchannel(使用二進制格式)。在某些情形下,httpchannel能夠無須打開端口(port)就可以通過防火墻,并且它支持標準的安全和驗證協議。
你可以使用任何類型的應用域(windows forms應用、asp.net web應用、控制臺應用、windows服務和其它托管應用域)來創建監聽應用程序。因為遠程配置是基于每個應用域的,所以應用域必須進行請求監聽。
『注』不同于com,遠程處理框架不會為你啟動監聽應用或者服務器應用。
遠程配置可以在編程階段來實現,也可以使用應用程序或者機器的配置文件來實現。使用配置文件使你能夠改變遠程處理配置,而不用重新編譯你的可執行文件。見如下代碼:
[visual basic]
' listener.vb
imports system
imports system.runtime.remoting

public class listener
public shared sub main()
remotingconfiguration.configure("listener.exe.config")
console.writeline("listening for requests. press enter to exit...")
console.readline()
end sub 'main
end class 'listener
[c#]
// listener.cs
using system;
using system.runtime.remoting;

public class listener{
public static void main(){
remotingconfiguration.configure("listener.exe.config");
console.writeline("listening for requests. press enter to exit...");
console.readline();
}
}
為了使用.net framework sdk附帶的命令行工具來將上述的類編譯成監聽服務可執行文件,只要將它保存為listener.language-extension(此處language-extension是你所使用的語言,比如cs、vb)。保存文件到remotabletype.dll的同一目錄中。編譯時使用如下命令:
[visual basic]
vbc /r:remotabletype.dll listener.vb

[c#]
csc /noconfig /r:remotabletype.dll listener.cs

listener類必須能夠找到 listener.exe.config 文件來加載對于remotabletype類的配置。該配置文件必須保存到和listener.exe相同的目錄中。如果沒找到將會產生一個異常。下面是配置文件listener.exe.config 的描述:
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="singleton"
type="remotabletype, remotabletype"
objecturi="remotabletype.rem"
/>
</service>
<channels>
<channel ref="http" port="8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
遠程處理系統使用配置文件中的信息來監聽和路由對于遠程類型實例的遠程請求。該文件指定了單一的服務激活模式(server-activation mode)、類型名、所要監聽的類型所在的程序集和對象的uri(或者是對象的外部名)配置文件指定遠程處理系統用httpchannel在8989端口進行監聽。

三、創建客戶應用:
客戶應用程序必須進行注冊。遠程處理系統會截獲客戶程序的調用,將調用送到遠程對象,然后返回結果給客戶端。客戶應用的代碼如下:
[visual basic]
' client.vb
imports system
imports system.runtime.remoting

public class client
public shared sub main()
remotingconfiguration.configure("client.exe.config")
dim remoteobject as new remotabletype()
console.writeline(remoteobject.stringmethod())
end sub 'main
end class 'client
[c#]
// client.cs
using system;
using system.runtime.remoting;

public class client{

public static void main(){
remotingconfiguration.configure("client.exe.config");
remotabletype remoteobject = new remotabletype();
console.writeline(remoteobject.stringmethod());
}
}
要編譯產生相應的客戶端可執行文件,只要將它保存為client.language-extension(此處language-extension是你所使用的語言,比如cs、vb)。保存文件到remotabletype.dll的同一目錄中。編譯時使用如下命令:
[visual basic]
vbc /r:remotabletype.dll client.vb

[c#]
csc /noconfig /r:remotabletype.dll client.cs

如你所見,client類必須能夠找到client.exe.config 文件來加載對于remotabletype類的配置。該配置文件必須保存到和client.exe相同的目錄中。如果沒找到將會產生一個異常。下面是配置文件client.exe.config 的描述:
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="remotabletype, remotabletype"
url="http://localhost:8989/remotabletype.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
該配置文件通知遠程處理系統remotabletype遠程對象的類型信息可以在remotabletype程序集中找到。同時,客戶程序應該試圖創建和使用位于 http://localhost:8989/remotabletype.rem的remotabletype對象。當你試圖在網絡上運行該程序,必須用遠程計算機名來替換客戶配置文件中的“localhost”。

四、編譯并執行整個應用:
保存前面所建的所有文件到一個名為listener的目錄中,并使用如下命令行:
visual basic]
vbc /t:library remotabletype.vb

vbc /r:remotabletype.dll listener.vb

vbc /r:remotabletype.dll client.vb

[c#]
csc /noconfig /t:library remotabletype.cs

csc /noconfig /r:remotabletype.dll listener.cs

csc /noconfig /r:remotabletype.dll client.cs

運行該應用:
1、創建一個名為client的子目錄
2、復制remotabletype.dll、client.exe和 client.exe.config到client目錄中
3、在listener目錄下,使用如下命令:
listener
4、當listener程序運行時,在client目錄下打開一個新的command窗口,并鍵入:
client

改變通道:
只要修改 listener.exe.config 和client.exe.config文件就可以改變通道。client.exe.config 文件如下修改:
<wellknown
type="remotabletype, remotabletype"
url="tcp://localhost:8989/remotabletype.rem"
/>

listener.exe.config 文件中如下修改:
<channel ref="tcp" port="8989"/>

『注』本文中所涉及內容限于.net framework 1.x 。

,歡迎訪問網頁設計愛好者web開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁山县| 犍为县| 新泰市| 财经| 禹州市| 浦江县| 石嘴山市| 马尔康县| 巢湖市| 赫章县| 秀山| 河间市| 达拉特旗| 临清市| 星子县| 樟树市| 扶绥县| 布拖县| 拜泉县| 柯坪县| 安阳县| 电白县| 称多县| 永吉县| 益阳市| 南华县| 平泉县| 石景山区| 莆田市| 龙泉市| 乌拉特前旗| 丁青县| 罗甸县| 舒兰市| 玉龙| 南召县| 始兴县| 莱阳市| 英山县| 岳阳县| 孟州市|