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

时间: 2024-08-28 00:17:26

Castle 整合.NET Remoting的相关文章

NET Core 整合Autofac和Castle

NET Core 整合Autofac和Castle 阅读目录 前言: 1.ASP.NET Core中的Autofac 2.整合Castle的DynamicProxy 3.注意事项 回到目录 前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 回到目录 1.ASP.NET Core中的Autofac 首先在Project.json的Dependency节点为中添

Autofac 之 基于 Castle DynamicProxy2 的 Interceptor 功能

Autofac 结合 Castle DynamicProxy2 功能 Autofac 不仅作为轻量级高效的 IoC 容器,而且还能很好的与 Castle.DynamicProxy2 结合起来,实现 AOP 功能. 首先,我们需要定义拦截器,简单的定义可实现 Castle.DynamicProxy.IInterceptor 接口即可. 添加拦截器   定义好了拦截器后,如何应用到相关对象呢?有两种方式: 1)使用 Autofac.Extras.DynamicProxy2.InterceptAttr

何为.Net Remoting【转】

借助基维百科给它的定义如下: NET Remoting 是微软 .NET Framework 中的一种网络通讯技术,与 XML Web Service 不同的是,它可以使用 SOAP 以外的协定来通讯,而在伺服端和用户端之间所操作的方法近乎相同,用户端可以不必考虑使用的协定,即可存取伺服端所开放的物件.这个技术与是由Distributed COM所发展而来的,与DCOM最大的不同是,DCOM有限制使用 TCP Port,但.NET Remoting 可以选择使用 TCP 或 HTTP 的方式通讯

基于restful注解(spring4.0.2整合flex+blazeds+spring-mvc)&lt;一&gt;

摘自: http://www.blogjava.net/liuguly/archive/2014/03/10/410824.html 参考官网:1.http://livedocs.adobe.com/blazeds/1/blazeds_devguide/2.http://docs.spring.io/spring-flex/docs/1.5.2.RELEASE/reference/html/1)下载blazeds(turnkey4.0.x版本)网址:http://sourceforge.net/

用Spring.Services整合 thrift0.9.2生成的wcf中间代码-复杂的架构带来简单的代码和高可维护性

参考页面: http://www.yuanjiaocheng.net/CSharp/csharp-class.html http://www.yuanjiaocheng.net/CSharp/csharp-variable.html http://www.yuanjiaocheng.net/CSharp/Csharp-data-types.html http://www.yuanjiaocheng.net/CSharp/cshart-value-reference-type.html http:

CI CD系统整合

转载_CI 系统搭建:Git.Gerrit与Jenkins 2014-08-11 20:55 15678人阅读 评论(1) 收藏 举报 分类: 软件集成和项目管理(3) 目录(?)[+] 去年写的这五篇 CI 文章时候方便邮件测试,自己搞了一个 thstack.com 域名玩.当时也没在意,所有的文章里邮箱地址都是引用 @thstack.com 域名.让我没想到是,2014 年这个神奇的一年,thstack.com 会成为我们的公司名字和域名. 我想说的是,我们内部的邮件系统也在用 @thsta

spirng整合rmi

Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的任何对象必须实现该远程接口. 在spring整合Rmi中: 服务端使用了org.springframework.remoting.rmi.RmiServiceExporter RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务.通过把Bean包装在一个适

WCF、Net remoting、Web service概念及区别

Windows通信基础(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK). WCF就是微软对于分布式处理的 编程技术的集大成者,它将DCOM.Remoting.Web Service.WSE.MSMQ集成在一起,从而降低了分布式系统开发者的学习曲线,并统一了开发标准. WCF是建立在.Net Framework 2.0基础之上的,包含在.NET 3.0/3.5

DWR整合Spring

1.前言 DWR提供了一个spring的创建器,一旦使用spring创建器,DWR将负责搜索Web应用中的Spring容器,并将Springp容器中的Bean转换成一个浏览器中JavaScript可调用的对象. 2.创建Java类 下面我们创建一个简单的服务器处理类,该服务器处理将被配置在Spring容器中. package com.owen.dwr.dwr; /** *服务器处理类 * @author owenwilliam 2016-5-8 * @version 1.0 */ public