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

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 構建基本的.NET Remoting應用程序 (http://www.webasp.net/article/18/17277.htm)
-- 作者:未知
-- 發佈日期: 2005-03-31
構建一個使用.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 。


webasp.net