今天研究了一下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();

}

}

}

新航道雅思

转载于:https://blog.51cto.com/zhangtaoze/1912847

Castle 整合.NET Remoting相关推荐

  1. ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

    前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 1.ASP ...

  2. Castle Team宣布Castle将与ASP.NET MVC整合

    MS宣布推出ASP.NET MVC Framework:http://www.cnblogs.com/Dah/archive/2007/10/08/916958.html 看了看上面的特性,概念几乎是 ...

  3. Spring整合Hessian

    Spring整合Hessian from:http://lavasoft.blog.51cto.com/62575/191871 Spring让Hessian变得不但强大,而且易用,但是易用背后,却有 ...

  4. 分布式系统设计原理与方案Dubbo+Zookeeper+Spring整合

    2019独角兽企业重金招聘Python工程师标准>>> 一直在思考分布式系统设计的问题,业务对象原封不动的情况下部署在客户端和服务器端,可以根据配置文件选择是连接服务器还是连接本地的 ...

  5. Castle DynamicProxy基本用法(AOP)

    本文介绍AOP编程的基本概念.Castle DynamicProxy(DP)的基本用法,使用第三方扩展实现对异步(async)的支持,结合Autofac演示如何实现AOP编程. AOP 百科中关于AO ...

  6. 六:Dubbo与Zookeeper、SpringMvc整合和使用

    DUBBO与ZOOKEEPER.SPRINGMVC整合和使用 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架 ...

  7. Dora.Interception,为.NET Core度身打造的AOP框架 [5]:轻松地实现与其他AOP框架的整合...

    这里所谓的与第三方AOP框架的整合不是说改变Dora.Interception现有的编程,而是恰好相反,即在不改变现有编程模式下采用第三方AOP框架或者自行实现的拦截机制.虽然我们默认提供基于IL E ...

  8. BlazeDS 整合 Flex HelloWorld 示例

    开发环境: System:Windows WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5.0.2.8.tomcat6 IDE:eclipse.MyEcl ...

  9. 基于.net平台remoting、DB2技术的大型分布式HIS系统架构及开发(项目架构师方向)...

    课程链接:http://***/goods.php?id=131 培训定位: 适合于.net方向有一定的工作经验提高用.本次课程涉及到系统架构.DB2.抽象工厂.组件式编程.基于角色和个人的混合授权思 ...

最新文章

  1. 【D3.js 学习总结】26、D3地理地图
  2. 教程-经典Delphi教程网
  3. Java 线程池框架核心代码分析--转
  4. linux cant open file for writing,linux 安装rz sz lrz lsz sftp: cannot open 文件名称 to write 报错解决...
  5. 26.删除排序数组中的重复项
  6. 小米手环nfc门卡摸拟成功后不能开门_一键开门,7种解锁方式,绿米推出全自动智能锁...
  7. opencv java水平投影_OpenCV实现图像在水平方向上投影
  8. jmeter 聚合报告说明_jmeter之聚合报告
  9. 基于R语言实现的交通时空大数据处理
  10. Linux系统升级硬盘后HOME分区扩容操作流程
  11. Web 项目中,MySQL 最新驱动下载、及配置
  12. win10隐藏任务栏_你真的了解任务栏吗?win10任务栏居然隐藏了这么多小窍门
  13. iPhone手机越狱不只是为了安装盗版应用、越狱的十大好处
  14. 招行权证割肉比赛开演 10个交易日后寿终正寝
  15. python 霍夫直线变换_霍夫线变换
  16. RGB与HSV颜色空间转换
  17. WPS公式和字体对不齐
  18. 如何在html添加css样式表,网页中添加CSS样式表的四种方式
  19. 小学校本培简讯 计算机技术,青石小学校本培训数学学科组简报
  20. 【python学习】-使用sklearn对数据进行线性回归,并绘制回归线

热门文章

  1. 【iOS开发】更改App图标下方显示的名称
  2. python set去重 字典 计算求和_python字典set方法的特殊方法
  3. 查看js 执行效率工具
  4. linux系统环境下压缩与解压缩
  5. mysql 多个字段拼接 concat
  6. 设置文件为源文件(和src一样)
  7. 21- vue django restful framework 打造生鲜超市 -首页商品分类显示功能
  8. thinkphp ajax 无刷新分页效果的实现
  9. 【解决方案】jquery live的change事件在IE下失效
  10. iPhone应用中APNS推送通知流程代码实现案例