·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » 构建基本的.NET Remoting应用程序

构建基本的.NET Remoting应用程序

类别: ASP.NET教程  评论数:0 总得分:0
构建一个使用.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 。




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1