Castle 整合.NET Remoting

简介:
今天研究了一下Castle的Remoting Facility.记录如下:
微软以前使用COM/DCOM的技术来处理分布式系统架构,通过Client端的Proxy代理程序来呼叫远程Server机器上的对象。.NET Framework则使用.NET Remoting或Web Services技术来实作分布式处理的工作概念;在这里针对.NET Remoting的设计架构做一个初步的简介和Castle整合示例。
.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方法代为计算总和。程序代码如下,并说明于后:
using  System;
 
namespace  RemoteSample.Components
{
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     public interface IRemoteMath
     {
         int Sum(params int[] a);
 
         int CallCounter
         {
              get;
         }
     }
}
 
using  System;
using  RemoteSample.Components;
 
namespace  RemoteSample.Components
{
     /// <summary>
     /// RemoteMath 的摘要说明。
     /// </summary>
     public class RemoteMath: MarshalByRefObject,IRemoteMath
     {
         private int callCounter = 0;
 
         public RemoteMath()
         {
             
         }
 
 
         #region  接口IRemoteMath的成员实现
         /// <summary>
         /// 求和计算
         /// </summary>
         /// <param name="a"></param>
         /// <returns></returns>
         public int Sum(params int[] a)
         {
              int sum = 0;
              for (int i = 0; i <= a.Length - 1; i++)
              {
                   sum += a[i];
              }
              callCounter += 1;
              return sum;
         }
 
        
         public int CallCounter
         {
              get
              {
                   return this.callCounter;
              }
         }
    
         #endregion
     }
}
 
说明: Remoting对象必须继承自MarshalByRefObject,这样才能通过网络,将对象执行个体的参考位置传递给呼叫端。
步骤2:在 Server上初始化Remoting对象,程序代码如下,并说明于后:
namespace  RemoteSample.Server
{
 
     class RemoteServerMain
     {
         [STAThread]
         internal static void Main(string[] args)
         {
              IWindsorContainer container = new RemotingContainer();
    
              Console.ReadLine();
         }
     }
}
ServerConfig.xml文件:
<? xml   version ="1.0"   encoding ="utf-8"   ?>
< configuration >
 
     <facilities>
         <facility id="remote.facility" type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting"
                   remotingConfigurationFile ="../../RemotingTcpConfig.config"
                isServer="true"
                 registryUri="kernel.rem" >
         </facility>
     </facilities>
    
     <components>
         <component
              id="remote.math"
              service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"
              type="RemoteSample.Components.RemoteMath, RemoteSample.Components"
              remoteserver="component"  >
         </component>
     </components>
 
</ configuration >
RemotingTcpConfig.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
       <application>
           <channels>
              <channel ref="tcp" port="2133" />
           </channels>
       </application>
    </system.runtime.remoting>
</configuration>
 
说明:
使用 Castle 的Remoting Facillity 使用Remoting 。
1.配置指出在 2133 port上要建立TCP Channel, 2133 port上要建立tcp Channel
 
2. <components>指出在Server端注册所要使用的组件、服务的名称及启动的方式。其中 component表示一个执行个体可供多个前端来呼叫,可保留其状态,另一种则为 ClientActivated,一个执行个体只能服务一个前端的呼叫,无法保留其状态。
步骤3:在 Client端使用Remoting对象
ClientConfig.xml
<? xml   version ="1.0"   encoding ="utf-8"   ?>
< configuration >
 
     <facilities>
         <facility
                   id="remote.facility"
                 type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting"
                 remotingConfigurationFile="../../RemotingTcpConfigClient.config"
                 isClient="true"
                 remoteKernelUri="tcp://localhost:2133/kernel.rem"
                 baseUri="tcp://localhost:2133" >
         </facility>
     </facilities>
 
  <components>
 
    <component
         id="remote.math"
         service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"
         type="RemoteSample.Components.RemoteMath, RemoteSample.Components"
         remoteclient="component" />
 
  </components>
 
</ configuration >
RemotingTcpConfigClient.config
<? xml   version ="1.0"   encoding ="utf-8"   ?>
< configuration >
     <system.runtime.remoting>
         <application>
              <channels>
                   <channel ref="tcp" port="0" />
              </channels>
         </application>
     </system.runtime.remoting>
</ configuration >
程序代码如下:

namespace  RemoteSample.Client
{
     /// <summary>
     /// RemoteClient的摘要说明。
     /// </summary>
     public class RemoteClientMain
     {
         [STAThread]
         static void Main(String[] args)
         {
              IWindsorContainer container = new RemotingContainer();
              IRemoteMath remoteMath = (IRemoteMath)container[typeof(IRemoteMath)] ;
              Console.WriteLine("Client1 TCP Call Sum method {0} Counter {1}",remoteMath.Sum(10, 20, 30),remoteMath.CallCounter);
 
              Console.WriteLine("....press a key to stop");
              Console.ReadLine();
         }
     }
}
代码下载:RemotingSamples.rar





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/75212,如需转载请自行联系原作者
目录
相关文章
|
C# 数据安全/隐私保护 开发者
『.NET』.NET 中常用的AOP框架——Castle
📣读完这篇文章里你能收获到 - AOP概念介绍 - 结合具体代码讲解.NET项目接入Castle
255 0
『.NET』.NET 中常用的AOP框架——Castle
|
网络安全 Windows 数据库
.Net Remoting的双向通信和Windows Service的宿主服务
原文:.Net Remoting的双向通信和Windows Service的宿主服务      作为微软分布式技术之一的.Net Remoting,从性能、安全等各方面来说都是相对比较稳定的,也是一项比较成熟的分布式技术。
894 0
|
网络协议 网络架构
.Net中Remoting通信机制
原文:.Net中Remoting通信机制 Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式。
830 0
|
C# 网络协议
.Net中Remoting通信机制简单实例
原文:.Net中Remoting通信机制简单实例 .Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例     本程序采用语言:c#   编译工具:vs2013工程文件   编译环境:.
810 0
.NET Remoting学习笔记(二)激活方式
原文:.NET Remoting学习笔记(二)激活方式 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道   参考:百度百科  风车车.Net   激活方式概念 在访问远程类型的一个对象实例之前,必须通过一个名为Activation的进程创建它并进行初始化。
951 0
|
网络协议
.NET Remoting学习笔记(一)概念
原文:.NET Remoting学习笔记(一)概念 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道  背景 自接触编程以来,一直听过这个名词Remoting,但是对他了解少之又少,近日有点时间,参考研究研究。
1129 0
|
网络协议 数据安全/隐私保护 网络架构
.NET Remoting学习笔记(三)信道
原文:.NET Remoting学习笔记(三)信道 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道   参考:风车车.Net   .NET Framework 远程处理基础结构提供下列信道实现: IpcChannel TcpChannel HttpChannel IpcChannel IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。
930 0