重温.NET Remoting(一)
.NET Remoting是一种分布式技术,当客户端调用远程服务端对象的时候,取得一个Proxy对象,对此对象的操作将被Formatter转换成SOAP或Binary消息,然后通过通信协议Http或者Tcp传到远程服务端。服务端将调用Dispatcher组件,将消息分发到不同的Message Sink对调用做出反应。
.NET Remoting对象的创建方式有两种,SAO(Server Activate Object)服务端激活 和CAO(Client Activate Object)客户端激活,其中SAO可以通过RegisterConfiguration.RegisterWellKnownType将对象创建为WellKnownObjectMode.Singleton模式或WellKnownObjectMode.SingleCall模式。
其中前者将会对所有的调用请求启用同一个服务对象,而后者将对不同的请求启用不同的服务对象。SAO调用示例可以通过共享一个数据契约程序集(契约的实现不必提供给客户端)供Client和Server端调用。
Ø SAO调用示例
服务契约
using System.Collections.Generic;
using System.Text;
namespace ShareDLL
{
public interface IMyService
{
string SayHello(string name);
}
[Serializable]
public class PersonData
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
public PersonData(string name, int age)
{
this._name = name;
this._age = age;
}
}
}
服务端
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Server
{
class Program
{
static void Main(string[] args)
{
TcpChannel chnl = new TcpChannel(9012);
ChannelServices.RegisterChannel(chnl, false);
//SingleCall方式
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyServiceImpl),
"myservice.rem",
WellKnownObjectMode.SingleCall);
//Singleton方式
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyServiceImpl),
// "myservice.rem",
// WellKnownObjectMode.Singleton);
Console.WriteLine("Server is Running...");
Console.Read();
}
}
public class MyServiceImpl : MarshalByRefObject,ShareDLL.IMyService
{
public string SayHello(string name)
{
return string.Format(@" Message from Server :"+ name);
}
}
}
客户端
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Client
{
class Program
{
static void Main(string[] args)
{
TcpChannel chnl = new TcpChannel();
ChannelServices.RegisterChannel(chnl, false);
ShareDLL.IMyService service=(ShareDLL.IMyService)Activator.GetObject(typeof(ShareDLL.IMyService),
"tcp://localhost:9012/myservice.rem");
Console.WriteLine(service.SayHello("code6421"));
Console.Read();
}
}
}
关于CAO创建对象,用三种方式,
Ø Share Implement共享数据契约与实现。即将契约的定义与实现放在一个程序集,然后供服务端和客户端引用。
Ø Soapsuds通过元数据生成供客户端使用的dll。即将契约的定义与实现放在一个程序集,然后通过通过工具将服务生成一个dll,供客户端引用。
Soapsuds –url:http://localhost:9012/myservice.rem?wsdl –oa:d:\share.dll,在应用此工具的时候,
*然后通过Soapsuds –url:http://localhost:9012/myservice.rem?wsdl –oa:d:\share.dll,
*生成供客户端应用的dll
*/
HttpChannel chnl = new HttpChannel(9012);
ChannelServices.RegisterChannel(chnl, false);
RemotingConfiguration.ApplicationName = "MyServer";
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CAO.ShareDLL.MyService),
"myservice.rem",
WellKnownObjectMode.SingleCall);
Ø Client Object Factory,通过客户端调用服务端的对象工厂,创建服务对象。
契约层:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAO.Factory.ShareDLL
{
public interface IMyService
{
string SayHello(string name);
ICAOObject CreateCAO(string name);
}
public interface ICAOObject
{
string Name { get; set; }
string SayHelloCAO();
}
}
服务端:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace CAO.Factory.Server
{
class Program
{
static void Main(string[] args)
{
TcpChannel chnl = new TcpChannel(9012);
ChannelServices.RegisterChannel(chnl, false);
//SingleCall方式
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyService),
"myservice.rem",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Server is Running...");
Console.Read();
}
}
public class MyService:MarshalByRefObject,ShareDLL.IMyService
{
public string SayHello(string name)
{
return string.Format("Hello {0}",name);
}
public ShareDLL.ICAOObject CreateCAO(string name)
{
return new MyCAOImpl(name);
}
}
public class MyCAOImpl : MarshalByRefObject, ShareDLL.ICAOObject
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string SayHelloCAO()
{
return string.Format("Hello {0}", this.Name);
}
public MyCAOImpl(string name)
{
this._name = name;
}
}
}
客户端:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CAO.Factory.Client
{
class Program
{
static void Main(string[] args)
{
TcpChannel chnl = new TcpChannel();
ChannelServices.RegisterChannel(chnl, false);
ShareDLL.IMyService service = (ShareDLL.IMyService)Activator.GetObject(typeof(ShareDLL.IMyService),
"tcp://localhost:9012/myservice.rem");
ShareDLL.ICAOObject obj1 = service.CreateCAO("mike");
Console.WriteLine(obj1.SayHelloCAO());
Console.Read();
}
}
}
以上是《Framework的架构设计与应用》关于Remoting的读书笔记。全部源代码下载: http://files.cnblogs.com/jackhuclan/RemotingTest.rar,关于更多高深的话题,可以参考:http://www.cnblogs.com/rickie/category/5082.html
转载于:https://www.cnblogs.com/jackhuclan/archive/2011/08/14/2137741.html
重温.NET Remoting(一)相关推荐
- Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务
Microsoft .Net Remoting系列专题之二 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...
- web service 和 remoting 有什么区别
其实现的原理并没有本质的区别,在应用开发层面上有以下区别: 1.Remoting可以灵活的定义其所基于的协议,如果定义为HTTP,则与Web Service就没有什么区别了,一般都喜欢定义为TCP,这 ...
- 远程处理Remoting
日程 ?应用程序域 ?Remoting和原理 ?编程式和管理式配置实例 用应用程序域 操作系统和运行库环境通常会在应用程序间提供某种形式的隔离.例如,Microsoft Windows 使用进程来隔离 ...
- .NET Remoting中的通道注册
今天我的同事使用Remoting注册一个新通道.奇怪的是,通道始终无法注册,总是报告异常"该通道已被占用".我明白这个异常出现的原因,但不明白的是此时系统并未使用任何一个通道,为何 ...
- 用Remoting 实现一个文件传输组件
为了传送文件,用remoting 实现很简单容易,有工程源码和演示程序下载,是从我写的一个网络库的一个子模块:有注解,不加以文字说明了. /**//* 作者:S.F. blog:www.cnblogs ...
- Flex与ASP.NET通过Remoting方式进行通讯
前两天研究了一下Flex与.NET如何进行数据交互,并写了一个文档,后面叙述得还不是很详细,还可以再研究深一点.本文是关于Flex与ASP.NET通过Remoting方式进行通讯的内容,过段时间有空还 ...
- 重温ES6核心概念和基本用法
ES6在2015年6月就得以批准,至今已两年了.近一年多以来陆续看过很多ES6的资料,工作项目中也逐步的用上了很多ES6的特性(let,const,promise,Template strings,C ...
- .net Remoting(2)——信道,MarshalByRefObject类
remoting提供一种允许对象通过应用程序域去与另一对象进行交互的框架.如果要通过应用程序域进行通信,在remoting中要通过信道(channel)来实现. 信道 信道是跨越远程处理边界(应用程序 ...
- 使用remoting远程控制编译机
远程对象服务器 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using S ...
- 据lovecherry的一步一步学Remoting序列文章学习.net Remoting日记(2)
今天学习了服务器端激活和客户端激活的区别!可还是出现了一点点的差错,经过对比得到正确的调用方法,整理如下: 1.服务器端激活,分为两种方式Singleton和SingleCall方式 Server端A ...
最新文章
- 数据库 文件 备份【学习 使用】
- Redis常见数据类型_Redis通用指令
- 惯性矩和偏心距描述器
- 为什么大多数程序员都抽烟_为什么大多数重新设计都会失败
- 论MVVM伪框架结构和MVC中M的实现机制
- js 高级-创建对象
- Atitit 2019技术趋势与没落技术 目录 1.1. abcdAtitit 技术领域趋势 abcd研究总结AI(人工智能)BlockChain(区块链)、Cloud(云)、和Data(大数据)
- SSH免密码登录,搭建Flink standalone集群
- hp usb disk storage format tool
- u盘ios刻录_用UltraISO刻录U盘安装系统
- oracle 052 题库变了,oracle ocp题库变化,052新加的考试题收集整理-30
- 数据包络分析DEA有哪些指标?
- 网页导出pdf不完整_偶尔偷个懒吧:3种pdf文献全文一键翻译的方法
- 【JZOJ3337】wyl8899的TLE【二分】【哈希】
- 笔记本闪屏是怎么回事呢?笔记本闪屏三个原因介绍
- “年薪百万”的视频剪辑师?Adobe专家让这一切都成为可能
- 世界50所知名大学开放课程列表及对应网站
- 一秒批量修改文件扩展名(后缀名)
- 拿到一个网站,怎么判断该网站是否存在sql注入漏洞?
- 前端基础-VUE入门教程(一)
热门文章
- LINUX修改.bashrc之后,生效的办法
- Template Method模板方法
- oracle查询多个加锁,解析oracle对select加锁的方法以及锁的查询 转
- 从事计算机工作的应该,未来想从事计算机方面的工作,现在应该学习些什么东西?...
- python3 获取文件目录_python3--os.path获取当前文件的绝对路径和所在目录
- python 整除的数组_LeetCode 974. 和可被 K 整除的子数组 | Python
- eclipse打包成jar_SpringBoot系列(三)- 用 jar的方式运行springboot项目
- python变量使用前必须先声明_Python变量使用前必须先声明,并且一旦声明就不能再当前作用域内改变其类型。_学小易找答案...
- 天津农学院计算机科学与技术在那个校区,天津农学院有几个校区及校区地址
- 背景色渐变html代码,求html文字背景色渐变的代码