轉自:http://www.iiiedu.org.tw/knowledge/knowledge20030430_2.htm

.NET Remoting

資策會數位教育研究所講師 董淑惠

 

 

概念簡介
微軟以往使用COM/DCOM的技術來處理分散式系統架構,透過Client端的Proxy代理程式來呼叫遠端Server機器上的物件。.NET Framework則使用.NET Remoting或Web Services技術來實作分散式處理的工作概念;在此針對.NET Remoting的設計架構做一個初步的簡介。

.NET Framework提供了多種的機制來支援Remoting,如:
.利用Channel來負責訊息的傳送與接收。
.利用Formatter來負責在訊息要透過channel傳送出去之前,先將訊息做適當的加密,或於訊息在透過Channel接收進來之後,先將訊息做相對的解密工作。
.利用Proxy來呼叫遠端的物件執行所要的功能呼叫。

其關係如下圖所示:

Channel 和 Formatter
在遠端物件被使用之前,必須先在Server端註冊好訊息傳送的通道(Channel),這些Channel可透過.NET Remotin configuration file或 ChannelServices物件類別的RegisterChannel方法來註冊。

在Channel的使用上,.NET Framework支援HTTP、TCP及SMTP等通道。若使用HTTP Channel ,則使用SOAP協定來收送訊息,所有的訊息會被傳送到SOAP Formatter中,被序列化(serialized)成XML的格式,而SOAP所需的headers也會被加入。至於使用TCP Channel者,則使用TCP協定來將訊息傳送到Binary Formatter中,以Binary Stream的方式來將訊息傳送到URI目的地。(URI : Universal Resource Identifier,類似大家所熟悉的URL)。

Activation and Proxy
Server-Side Activation
Server端在Client端要存取Remoting物件時必需在Server端能自動啟始Remoting物件,可使用RemotingConfiguration物件類別的RegisterWellKnownServiceType方法來完成這項工作。

Client-Side Activation
Client端要使用遠端物件之前,可使用New 或Activator 物件類別所提供的CreateInstance或GetObject方法來啟動物件並傳回Proxy,以便Client端可透過Proxy來執行叫用遠端物件的方法。

範例
以下分三個步驟來介紹
1. 建立Remoting物件
2. 在Server上初始Remoting物件
3. Client端使用Remoting物件

步驟1:建立Remoting物件
建立一個MathServer物件類別,提供Sum方法,可給予一連串的整數由Sum方法代為計算總和。程式碼如下,並說明於後:
Imports System
Namespace RemotingSamples

Public Class MathServer
          Inherits MarshalByRefObject

Public callCounter As Integer = 0

Function Sum(ByVal ParamArray a() As Integer) As Integer
               Dim i As Integer
               For i = 0 To a.Length - 1
                    Sum += a(i)
               Next

callCounter += 1
     End Function
End Class

End Namespace
說明:Remoting物件必須繼承自MarshalByRefObject,如此才能透過網路,將物件執行個體的參考位置傳遞給呼叫端。

步驟2:在Server上初始Remoting物件,程式碼如下,並說明於後:

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports ObjectServices.RemotingSamples

Public Class Server
     Public Shared Sub Main()
               ‘建立兩個通道
          Dim chan1  As New Tcp.TcpChannel(8085)
          Dim chan2 As New Http.HttpChannel(8086)

‘註冊要偵聽這兩個通道
          ChannelServices.RegisterChannel(chan1)
          ChannelServices.RegisterChannel(chan2)

‘設定啟動哪個元件、服務的名稱及啟動的方式
                    ' 方法一
                    RemotingConfiguration.RegisterWellKnownServiceType( _
                         GetType(ObjectServices.RemotingSamples.MathServer), _
                         "CallMathFunction", _
                         WellKnownObjectMode.Singleton)

' 方法二
                    ' RemotingConfiguration.RegisterWellKnownServiceType( _
                    '          GetType(ObjectServices.RemotingSamples.MathServer), _
                    '          "CallMathFunction", _
                    '          WellKnownObjectMode.SingleCall)

Console.WriteLine("Press Enter key to exit")
                    Console.ReadLine()
          End Sub
End Class
說明:
1. Dim chan1 As New Tcp.TcpChannel(8085)
      Dim chan2 As New Http.HttpChannel(8086)
指出在8085 port上要建立TCP Channel, 8086 port上要建立Http Channel

2. ChannelServices.RegisterChannel(chan1)
    ChannelServices.RegisterChannel(chan2)
註冊要偵聽 Chan1 和 Chan2

3. RemotingConfiguration.RegisterWellKnownServiceType( GetType(ObjectServices.RemotingSamples.MathServer), "CallMathFunction",WellKnownObjectMode.Singleton)

指出在Server端註冊所要使用的元件、服務的名稱及啟動的方式。
其中WellKnownObjectMode.Singleton表示一個執行個體可供多個前端來呼叫,可保留其狀態,另一種則為WellKnownObjectMode.SingleCall,一個執行個體只能服務一個前端的呼叫,無法保留其狀態。

步驟3:在Client端使用Remoting物件,程式碼如下:
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports Microsoft.VisualBasic
Imports System.IO
Imports ObjectServices.RemotingSamples

Public Class Client
     Public Shared Sub Main()

Dim counter As Integer

Dim chan1 As New TcpChannel()
          ChannelServices.RegisterChannel(chan1)

Dim obj1 As MathServer = _
               CType(Activator.GetObject( _
                    GetType(ObjectServices.RemotingSamples.MathServer), _
                    "tcp://localhost:8085/CallMathFunction"), _
                    MathServer)

If (obj1 Is Nothing) Then
               Console.WriteLine("Could not locate TCP server")
               Exit Sub
          End If

Dim chan2 As New HttpChannel()
          ChannelServices.RegisterChannel(chan2)

Dim obj2 As MathServer = _
               CType(Activator.GetObject( _
                    GetType(ObjectServices.RemotingSamples.MathServer), _
                    "http://localhost:8086/CallMathFunction"), _
                    MathServer)

If (obj2 Is Nothing) Then
               Console.WriteLine("Could not locate HTTP server")
               Exit Sub
          End If

Try
               Console.WriteLine("Client1 TCP Call Sum method {0} Counter {1}", obj1.Sum(10, 20, 30), obj1.callCounter)
               Console.WriteLine("Client2 HTTP HelloMethod {0} Counter {1}", obj2.Sum(100, 200, 300, 400), obj1.callCounter)
          Catch ioExcep As IOException
               Console.WriteLine("Remote IO Error" & vbCrLf & "Exception:" & vbCrLf & ioExcep.ToString())
               End Try
          End Sub

End Class

說明:
1.Dim obj1 As MathServer = _
                    CType(Activator.GetObject( _
                         GetType(ObjectServices.RemotingSamples.MathServer), _
                         "tcp://localhost:8085/CallMathFunction"), _
                         MathServer)
在Tcp道路上叫用遠端物件(含遠端物件的物件型別名稱、URI及通道資料),透過Activator.GetObject來起始物件並傳回Proxy。

原始程式碼下載

.NET Remoting相关推荐

  1. Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务

    Microsoft .Net Remoting系列专题之二 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...

  2. web service 和 remoting 有什么区别

    其实现的原理并没有本质的区别,在应用开发层面上有以下区别: 1.Remoting可以灵活的定义其所基于的协议,如果定义为HTTP,则与Web Service就没有什么区别了,一般都喜欢定义为TCP,这 ...

  3. 远程处理Remoting

    日程 ?应用程序域 ?Remoting和原理 ?编程式和管理式配置实例 用应用程序域 操作系统和运行库环境通常会在应用程序间提供某种形式的隔离.例如,Microsoft Windows 使用进程来隔离 ...

  4. .NET Remoting中的通道注册

    今天我的同事使用Remoting注册一个新通道.奇怪的是,通道始终无法注册,总是报告异常"该通道已被占用".我明白这个异常出现的原因,但不明白的是此时系统并未使用任何一个通道,为何 ...

  5. 用Remoting 实现一个文件传输组件

    为了传送文件,用remoting 实现很简单容易,有工程源码和演示程序下载,是从我写的一个网络库的一个子模块:有注解,不加以文字说明了. /**//* 作者:S.F. blog:www.cnblogs ...

  6. Flex与ASP.NET通过Remoting方式进行通讯

    前两天研究了一下Flex与.NET如何进行数据交互,并写了一个文档,后面叙述得还不是很详细,还可以再研究深一点.本文是关于Flex与ASP.NET通过Remoting方式进行通讯的内容,过段时间有空还 ...

  7. .net Remoting(2)——信道,MarshalByRefObject类

    remoting提供一种允许对象通过应用程序域去与另一对象进行交互的框架.如果要通过应用程序域进行通信,在remoting中要通过信道(channel)来实现. 信道 信道是跨越远程处理边界(应用程序 ...

  8. 使用remoting远程控制编译机

    远程对象服务器 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using S ...

  9. 据lovecherry的一步一步学Remoting序列文章学习.net Remoting日记(2)

    今天学习了服务器端激活和客户端激活的区别!可还是出现了一点点的差错,经过对比得到正确的调用方法,整理如下: 1.服务器端激活,分为两种方式Singleton和SingleCall方式 Server端A ...

  10. Remoting Practice Sample

    Remoting Practice Sample 这两天研究了一下Remoting, 改了MSDN 的例子. 那个例子不是很方便. 我做了如下改进: 整个sample做成一个solution 用了wi ...

最新文章

  1. [企业化NET]Window Server 2008 R2[3]-SVN 服务端 和 客户端 基本使用
  2. vue中computed(计算属性)和watch在实现父子组件props同步时的实际区分
  3. 《编程珠玑》笔记3 数据结构选择
  4. android客户端和服务器实现传图片和文件
  5. virtualbox下安装archlinux
  6. LeetCode - 35. Search Insert Position
  7. 设备log导入oracle,oracle imp 导入数据库出错 请看log!
  8. 新库上线 | CnOpenData中国工业企业绿色专利及引用被引用数据简介
  9. 软件测试用例.范文,软件测试用例模板范文
  10. python属于汇编语言还是高级语言_python语言属于汇编语言吗?_后端开发
  11. 种草电商系统种草电商系统开发解决方案
  12. 维多利亚计算机研究生,2020年惠灵顿维多利亚大学计算机信息硕士申请条件
  13. 计算机网络最佳路由,计算机网络路由研究分析
  14. 第五届阿里天池中间件比赛经历分享
  15. 因果推理(五):随机试验和可识别
  16. Chrome浏览器未连接到互联网的解决办法
  17. java计算机毕业设计淮安城市开放大学实习实训管理系统源码+mysql数据库+系统+lw文档+部署
  18. 现货黄金短线技巧要点
  19. Unity UGUI图集打包与动态使用(TexturePacker)
  20. 【HJ42 学英语】C++

热门文章

  1. 虚拟机网络配置详解(NAT、桥接、Hostonly)
  2. HTML5video 标签
  3. 8.2设备文件及磁盘分区
  4. xcode 设置快捷键 整行上下移动
  5. 使用ndk standalone工具链来编译某个平台下的库
  6. centos 下 mysql 主从库搭建
  7. JAVA编程经验汇总 (载)
  8. 4-3逻辑非运算符及案例 4-4
  9. js中 给json对象添加属性和json数组添加元素
  10. Memcached 在linux上安装笔记