.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 ...
随机推荐
- kailli linux download
https://www.offensive-security.com/kali-linux-arm-images/ Courses Certifications Online Labs Penetra ...
- RabbitMQ学习系列(三): C# 如何使用 RabbitMQ
上一篇已经讲了Rabbitmq如何在Windows平台安装,还不了解如何安装的朋友,请看我前面几篇文章:RabbitMQ学习系列一:windows下安装RabbitMQ服务 , 今天就来聊聊 C# 实 ...
- java enum
小谈Java Enum的多态性 博客分类: Java JavaAppleJDKJVMIDEA Enum+多态,我没说错,不过Enum是不可以被继承的,也不可以继承自别人,只是能实现接口而已,何谈多态 ...
- iPhone屏幕尺寸/launch尺寸/icon尺寸
屏幕尺寸 6p/6sp 414 X 736 6/6s 375 X 667 5/5s 320 X 568 4/4s 320 X 480 la ...
- 关于JSF中immediate属性的总结(二)
The immediate attribute in JSF is commonly misunderstood. If you don't believe me, check out Stack O ...
- C# Byte[] 转String 无损转换
C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...
- CSS解决未知高度垂直居中
尽管有CSS的vertical-align特性,但是并不能有效解决未知高度的垂直居中问题(在一个DIV标签里有未知高度的文本或图片的情况下). 标准浏览器如Mozilla, Opera, Safari ...
- 关于Access restriction: The type 'Application' is not API (restriction on required library)
原文链接:http://rxxluowei.iteye.com/blog/671893 今天写第一次写JavaFX的入门程序就GG 遇到了导入API的问题,无奈疯狂地通过网络找解决方案.. 我的问题是 ...
- js基础总结
DOM 节点 document节点 nodeType:9 文本节点 nodeType:3 元素节点 nodeType:1 注释节点 nodeType:8 属性节点 nodeType:2 at ...
- Android Studio 导出jar包
不像在Eclipse,可以直接导出jar包.AndroidStudio只可以生成aar包. 在网上看到许多朋友问怎么可以像Eclipse一样导出jar包,其实我们只要知道它的原理就可以了. 用jar命 ...