.Net中Remoting通信机制
-
Remoting通信机制
Remoting介绍
主要元素
通道类型
激活方式
对象定义
Remoting介绍

主要元素
- 远程对象——远程对象是运行在服务器上的对象。客户端不能直接调用远程对象上的方法,而要使用代理。使用.NET,很容易把远程对象和本地对象区分开:即任何派生自 MarshalByRefObject 的类从来都不会离开它的应用程序域。客户端可以通过代理调用远程对象的方法。
- 信道——信道用于客户端和服务器之间的通信。信道包括客户端的信道部分和服务器的信道部分。.NET Framework 4 提供了 3 种信道类型,它们分别通过 TCP、HTTP 和IPC 进行通信。此外,还可以创建自定义信道,这些信道使用其他协议通信。
- 消息——消息被发送到信道中。消息是为客户端和服务器之间的通信而创建的。消息包含远程对象的信息、被调用方法的名称以及所有的参数。
- 格式化程序——格式化程序用于定义消息如何传输到信道中。.NET 4 有SOAP 格式化程序和二进制格式化程序。使用 SOAP 格式化程序可以与不是基于.NET Framework 的Web 服务通信。二进制格式化程序速度更快,可以有效地用在内部网环境中。当然,也可以创建自定义格式化程序。
- 格式化程序提供程序——格式化程序提供程序用于把格式化程序与信道关联起来。通过创建信道,可以指定要使用的格式化程序提供程序,格式化程序提供程序则定义把数据传输到信道中时所使用的格式化程序。
- 代理——客户端调用代理的方法,而不是远程对象的方法。代理分为两种:透明的代理和真实的代理。对于客户端,透明代理看起来与远程对象类似。在透明代理上,客户端可以调用远程对象实现的方法。然后,透明代理调用真实代理上的 Invoke()方法。Invoke()方法使用消息接收器把消息传递给信道。
- 消息接收器——消息接收器是一个侦听器(interceptor)对象,简称接收器。在客户端和服务器上都有侦听器。接收器与信道相关联。真实的代理使用消息接收器把消息传递到信道中,因此,在消息进入信道之前,接收器可以进行截获工作。根据接收器所处的位置,可以把接收器称为特使接收器(envoy sink)、服务器上下文接收器、对象上下文接收器等。
- 激活器——客户端可以使用激活器在服务器上创建远程对象,或者获取一个被服务器激活的对象的代理。
- RemotingConfiguration 类——该类是用于配置远程服务器和客户端的一个实用程序类。它可以用于读取配置文件或动态地配置远程对象。
- ChannelServices 类——该类是一个实用程序类,可用于注册信道并把消息分配到信道中
通道类型
TcpChannel
HttpChannel
激活方式
对象定义
服务器
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerRemoteObject.ServerObject),
"ServiceMessage",WellKnownObjectMode.SingleTon);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerRemoteObject.ServerObject),
"ServiceMessage",WellKnownObjectMode.SingleCall);
RemotingConfiguration.ApplicationName = "ServiceMessage";
RemotingConfiguration.RegisterActivatedServiceType(
typeof(ServerRemoteObject.ServerObject));
//获得当前已注册的通道;
IChannel[] channels = ChannelServices.RegisteredChannels;
//关闭指定名为MyTcp的通道;
foreach (IChannel eachChannel in channels)
{
if (eachChannel.ChannelName == "MyTcp")
{
TcpChannel tcpChannel = (TcpChannel)eachChannel;
//关闭监听;
tcpChannel.StopListening(null);
//注销通道;
ChannelServices.UnregisterChannel(tcpChannel);
}
}
客户端
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
ServerRemoteObject.ServerObject serverObj = (ServerRemoteObject.ServerObject)Activator.GetObject(
typeof(ServerRemoteObject.ServerObject), "tcp://localhost:8080/ServiceMessage");
RemotingConfiguration.RegisterActivatedClientType(
typeof(ServerRemoteObject.ServerObject),
"tcp://localhost:8080/ServiceMessage");
ServerRemoteObject.ServerObject serverObj = new ServerRemoteObject.ServerObject();
ServerObject(string pName,string pSex,int pAge)
{
name = pName;
sex = pSex;
age = pAge;
}
object[] attrs = {new UrlAttribute("tcp://localhost:8080/ServiceMessage")};
object[] objs = new object[];
objs[] = "wayfarer";
objs[] = "male";
objs[] = ;
object[] attrs = {new UrlAttribute("tcp://localhost:8080/EchoMessage")};
ObjectHandle handle = Activator.CreateInstance("ServerRemoteObject",
"ServerRemoteObject.ServerObject",attrs);
ServerRemoteObject.ServerObject obj = (ServerRemoteObject.ServerObject)handle.Unwrap();
基础补充
IDictionary tcpProp = new Hashtable();
tcpProp["name"] = "tcp9090";
tcpProp["port"] = ;
IChannel channel = new TcpChannel(tcpProp,
new BinaryClientFormatterSinkProvider(),
new BinaryServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel);
IDictionary httpProp = new Hashtable();
httpProp["name"] = "http8080";
httpProp["port"] = ;
IChannel channel = new HttpChannel(httpProp,
new SoapClientFormatterSinkProvider(),
new SoapServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel);
public interface IServerObject
{
Person GetPersonInfo(string name,string sex,int age);
}
public class ServerObject:MarshalByRefObject,IServerObject
{ ......}
public interface IServerObject
{
Person GetPersonInfo(string name,string sex,int age);
}
public interface IServerObjFactory
{
IServerObject CreateInstance();
}
public class ServerObject:MarshalByRefObject,IServerObject
{
public Person GetPersonInfo(string name,string sex,int age)
{
Person person = new Person();
person .Name = name;
person.Sex = sex;
person.Age = age;
return person;
}
}
public class ServerObjFactory:MarshalByRefObject,IServerObjFactory
{
public IServerObject CreateInstance()
{
return new ServerObject();
}
}
public interface IServerObject
{
Person GetPersonInfo(string name,string sex,int age);
}
public interface IServerObjFactory
{
IServerObject CreateInstance();
}
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerRemoteObject.ServerObjFactory),
"ServiceMessage",WellKnownObjectMode.SingleCall);
ServerRemoteObject.IServerObjFactory serverFactory =
(ServerRemoteObject.IServerObjFactory) Activator.GetObject(
typeof(ServerRemoteObject.IServerObjFactory),
"tcp://localhost:8080/ServiceMessage");
ServerRemoteObject.IServerObject serverObj = serverFactory.CreateInstance();
public class ServerObject:MarshalByRefObject
{
public ServerObject()
{
}
public Person GetPersonInfo(string name,string sex,int age)
{
Person person = new Person();
person .Name = name;
person.Sex = sex;
person.Age = age;
return person;
}
}
public class ServerObject:MarshalByRefObject
{
public ServerObj()
{
throw new System.NotImplementedException();
}
public Person GetPersonInfo(string name,string sex,int age)
{
throw new System.NotImplementedException();
}
}
.Net中Remoting通信机制的更多相关文章
- .Net中Remoting通信机制简单实例
.Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例 本程序采用语言:c# 编译工具:vs2013工程文件 编译环境:.net 4.0 程序模块: Test测试 ...
- TensorFlow中的通信机制——Rendezvous(一)本地传输
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 在TensorFlow源码中我们经常能看到一个奇怪的词——Rendezvous ...
- TensorFlow中的通信机制——Rendezvous(二)gRPC传输
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 本篇是TensorFlow通信机制系列的第二篇文章,主要梳理使用gRPC网络传 ...
- Android中的常见通信机制和Linux中的通信机制
Handler Handler是Android系统中的一种消息传递机制,起作用是应对多线程场景.将A进程的消息传递给B线程,实现异步消息处理.很多情况是将工作线程中需要更新UI的操作消息传递给UI主线 ...
- android中的通信机制总结
第一种:使用handler来进行通信 handler 大家可以把它想象成主线程(UI线程)的一个子线程,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑,handler ...
- Android中AIDL通信机制分析
一.背景 ·1.AIDL出现的原因 在android系统中,每一个程序都是运行在自己的进程中,进程之间无法进行通讯,为了在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需 ...
- ROS中的通信机制
http://www.ros.org/core-components/ Communications Infrastructure At the lowest level, ROS offers a ...
- flux沉思录:面向store和通信机制的前端框架
一.综述 Flux 被用来描述“单向”的数据流,且包含某些特殊的事件和监听器. 响应式编程是一种面向数据流和变化传播的编程范式 flux是响应式编程的一种? Flux 在本质上采用了模型-视图-控制器 ...
- Android中对消息机制(Handler)的再次解读
今天遇到一些关于在子线程中操作Handler的问题,感觉又要研究源代码了,但是关于Handler的话,我之前研究过,可以参考这篇文章:http://blog.csdn.net/jiangwei0910 ...
随机推荐
- 使用ab对nginx进行压力测试
nginx以高并发,省内存著称. 相信大多数安装nginx的同学都想知道自己的nginx性能如何. 我想跟大家分享下我使用ab工具的压力测试方法和结果, ab是针对apache的性能测试工具,可以只安 ...
- (转载)详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
郑重声明:原文转载于http://dengqi.blog.51cto.com/5685776/1223132 向好文章致敬!!! 一:MAC地址表详解 说到MAC地址表,就不得不说一下交换机的工作原理 ...
- yii2 可逆加密
加密: $data 是你要加密的内容, $secretKey 是你自己设置的salt, $encryptedData = Yii::$app->getSecurity()->encrypt ...
- layer——源码学习
一.根据源码的学习 发现创建弹窗:使用了一些div来组成 zindex 和 index 是自动生成. zindex 表示生成的层次关系 index 用来表示各个层的id 默认class名 h = [& ...
- [Machine Learning] 梯度下降法的三种形式BGD、SGD以及MBGD
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...
- Uva 11732 strcmp() Anyone?
strcmp() Anyone? Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- STL~heap
1.定义 堆:若将此序列所存储的向量R[1..n]看做是一棵完全二叉树的存储结构,则堆实质上是满足如下性质的完全二叉树 树中任一非叶子结点的关键字均不大于(或不小于)其子结点的关键字.分为大根数(默认 ...
- jQuery判断及更改checkbox状态
判断:jquery对象.prop("checked") 选中:jquery对象.prop("checked", true) 取消选中:jquery对象.remo ...
- Xcode LLDB 调试Tips
1. bt -- 显示当前线程调用堆栈 2. e -- 执行表达式,动态修改当前线程变量的值 3. frame variable -- 打印当前堆栈所有变量值 4. expr -O --lan ...
- 安装第三方APP好的站点及解除安全与隐私限制
一.解除安全与隐私限制的任何来源. http://bbs.feng.com/read-htm-tid-10714286.html 接下来,我们就打开终端,然后输入以下命令: sudo spctl ...