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

来源:互联网 发布:finale软件最新 编辑:程序博客网 时间:2024/05/01 23: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 。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我老婆起泡疹腿剌痛睡不着了怎么办 oppo耳机孔坏了怎么办 魅族耳机口坏了怎么办 控制线的报验资料怎么办 人失踪报警派出所不管怎么办 铁板的货架久了怎么办 干镀锌让环保查了怎么办 水管软管生锈了拧不动怎么办 镀锌管会生锈吗.怎么办 冷镀锌钢管生锈了怎么办 卖了过期的东西怎么办 喝到了假的饮料怎么办 烧汤总是溢锅怎么办 脚踩垃圾桶坏了怎么办 连衣裙特别容易起褶怎么办 施肥过度烧根了怎么办 农作物施用尿素发生肥害怎么办? 水稻尿素施多了怎么办 花施肥施多了怎么办 玉米被化肥烧了怎么办 撒施复合肥没有充分融化怎么办 绿箩化肥施多了怎么办 辣椒化肥施多了怎么办? 化肥施多了烧苗怎么办 绿植施肥施多了怎么办 盆栽肥料放多了怎么办 三环复合肥怎么办啊 母猪下崽后不吃食怎么办 猪自配料料槽不下怎么办 美甲边缘起翘怎么办 猪粪流到鱼塘里鱼死了怎么办啊 在基本农田建有机肥厂怎么办 有机肥厂的环评怎么办 织玻璃纤维网布环评怎么办 吃了受潮的奶粉怎么办 喝了受潮的奶粉怎么办 刚买的奶粉受潮怎么办 羊不小心吃了化肥怎么办 阿胶粉结成块了怎么办 半桶奶粉受潮了怎么办 眉粉受潮了结块怎么办