参考:♂风车车.Net
IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel。
IpcChannel 执行下列功能:
TcpChannel 类使用二进制格式化程序将所有消息序列化为二进制流,并使用 TCP 协议将该流传输至目标统一资源标识符 (URI)。
TcpChannel 执行下列功能:
HttpChannel 类使用 SOAP 协议在远程对象之间传输消息。所有消息都通过 SoapFormatter 传递,此格式化程序会将消息转换为 xml 并进行序列化,同时向数据流中添加所需的 SOAP 标头。如果还指定了二进制格式化程序,则会创建二进制数据流。随后,将使用 HTTP 协议将数据流传输至目标 URI。
HttpChannel 符合 SOAP 1.1 标准,它执行下列功能:
下面贴代码:
1.定义远程对象
using System;using System.Runtime.Remoting.Metadata;/*code 释迦苦僧*/namespace MessageMarshal{ /*创建发送消息委托*/ public delegate void SendMessageHandler(string messge); [Serializable] public class TestMessageMarshal : MarshalByRefObject { PRivate Guid ID { get; set; } /*新建对象实例时重新创建标识编号*/ public TestMessageMarshal() { ID = Guid.NewGuid(); } /*创建发送消息事件*/ public static event SendMessageHandler SendMessageEvent; /*发送消息*/ [SoapMethod(XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal#SendMessage")] public void SendMessage(string messge) { if (SendMessageEvent != null) SendMessageEvent(ID.ToString() + "/t" + messge); } }}
2.定义服务端
using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;using System.Runtime.Remoting.Channels.Ipc;using System.Runtime.Remoting.Channels.Tcp;using MessageMarshal;namespace TestRemotingServer{ /*code:释迦苦僧*/ class Program { static void Main(string[] args) { //IpcChannel channel_ipc = new IpcChannel("localhost:8226"); //HttpChannel channel_http = new HttpChannel(8226); TcpChannel channel_tcp = new TcpChannel(8226); /*注册通道服务端*/ ChannelServices.RegisterChannel(channel_tcp, false); RemotingConfiguration.applicationName = "test"; RemotingConfiguration.RegisterActivatedServiceType(typeof(TestMessageMarshal)); Console.WriteLine("started ..."); /*接收客户端事件*/ TestMessageMarshal.SendMessageEvent+=new SendMessageHandler(TestMessageMarshal_SendMessageEvent); Console.Read(); } static void TestMessageMarshal_SendMessageEvent(string messge) { Console.WriteLine(messge); } }}
3.定义客户端:
using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;using System.Runtime.Remoting.Channels.Ipc;using System.Runtime.Remoting.Channels.Tcp;using System.Threading;/*code 释迦苦僧*/namespace TestRemotingClient{ class Program { static void Main(string[] args) { //IpcChannel channel = new IpcChannel(); //HttpChannel channel_http = new HttpChannel(); TcpChannel channel_tcp = new TcpChannel(); ChannelServices.RegisterChannel(channel_tcp, false); /*注册通道 的 远程处理类型*/ //RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "ipc://localhost:8226/test"); //RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "http://localhost:8226/test"); RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "tcp://localhost:8226/test"); /*创建消息实体*/ MessageMarshal.TestMessageMarshal TestMessage = new MessageMarshal.TestMessageMarshal(); while (true) { TestMessage.SendMessage("DateTime.Now:" + System.DateTime.Now.ToString()); Console.WriteLine("send message..."); Thread.Sleep(2000); } } }}
4.测试
.NET Remoting 咱只写三篇 应该能给大家带来些了解,应对面试
作者:释迦苦僧 出处:http://www.VEVb.com/woxpp/p/3997984.html 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
新闻热点
疑难解答