目录
参考:♂风车车.Net
.NET Framework 远程处理基础结构提供下列信道实现:
IpcChannel
IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel。
IpcChannel 执行下列功能:
TcpChannel
TcpChannel 类使用二进制格式化程序将所有消息序列化为二进制流,并使用 TCP 协议将该流传输至目标统一资源标识符 (URI)。
TcpChannel 执行下列功能:
HttpChannel
HttpChannel 类使用 SOAP 协议在远程对象之间传输消息。所有消息都通过 SoapFormatter 传递,此格式化程序会将消息转换为 XML 并进行序列化,同时向数据流中添加所需的 SOAP 标头。如果还指定了二进制格式化程序,则会创建二进制数据流。随后,将使用 HTTP 协议将数据流传输至目标 URI。
HttpChannel 符合 SOAP 1.1 标准,它执行下列功能:
下面贴代码:
1.定义远程对象
using System;
using System.Runtime.Remoting.Metadata; 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.Tcp; namespace TestRemotingServer
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("创建HTTP通道"); /*创建HTTP通道*/
TcpChannel channel = new TcpChannel(816); /*注册通道服务端*/
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.ApplicationName = "test"; /*服务端注册,使用SingleCall激活*/
RemotingConfiguration.RegisterActivatedServiceType(typeof(MessageMarshal.TestMessageMarshal)); Console.WriteLine("started ..."); /*接收客户端事件*/
MessageMarshal.TestMessageMarshal.SendMessageEvent += new MessageMarshal.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.Tcp;
using System.Threading; namespace TestRemotingClient
{
class Program
{
static void Main(string[] args)
{
/*创建通道*/
TcpChannel channel = new TcpChannel(); /*注册通道*/
ChannelServices.RegisterChannel(channel, false); /*注册通道 的 远程处理类型*/
RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "tcp://localhost:816/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.测试
定义为TcpChannel 时
定义服务端:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc; namespace TestRemotingServer
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("创建ICP通道"); /*创建HTTP通道*/
IpcServerChannel channel = new IpcServerChannel("Server"); /*注册通道服务端*/
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.ApplicationName = "test"; /*客户端激活*/
RemotingConfiguration.RegisterActivatedServiceType(typeof(MessageMarshal.TestMessageMarshal)); /*接收客户端事件*/
MessageMarshal.TestMessageMarshal.SendMessageEvent += new MessageMarshal.SendMessageHandler(TestMessageMarshal_SendMessageEvent); Console.Read();
} static void TestMessageMarshal_SendMessageEvent(string messge)
{
Console.WriteLine(messge);
}
}
}
定义客户端:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading; namespace TestRemotingClient
{
class Program
{
static void Main(string[] args)
{
/*创建通道*/
IpcClientChannel channel = new IpcClientChannel(); /*注册通道*/
ChannelServices.RegisterChannel(channel, false); /*注册通道 的 远程处理类型*/
RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "ipc://Server/test"); /*创建消息实体*/
MessageMarshal.TestMessageMarshal TestMessage = new MessageMarshal.TestMessageMarshal(); while (true)
{
TestMessage.SendMessage("DateTime.Now:" + System.DateTime.Now.ToString());
Console.WriteLine("send message...");
Thread.Sleep(2000);
}
}
}
}
运行结果
.NET Remoting 咱只写三篇 应该能给大家带来些了解,应对面试
作者:释迦苦僧 出处:http://www.cnblogs.com/woxpp/p/3997984.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。