让.NET Remoting更快些-IPCChannel的实现
来源:互联网 发布:volatile java的使用 编辑:程序博客网 时间:2024/04/29 19:11
IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel。
下面让我们来看看如何使用IPCChannel:
首先我们定义一个RemotingObject类:
using System;
// 远程对象
public class RemoteObject : MarshalByRefObject
...{
private int callCount = 0;
public int GetCount()
...{
Console.WriteLine("GetCount has been called.");
callCount++;
return(callCount);
}
}
// 远程对象
public class RemoteObject : MarshalByRefObject
...{
private int callCount = 0;
public int GetCount()
...{
Console.WriteLine("GetCount has been called.");
callCount++;
return(callCount);
}
}
接下来我们编写服务端代码:
using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Server
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道
IpcChannel serverChannel = new IpcChannel("TestChannel");
// 注册这个IPC信道.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(serverChannel);
// 打印这个信道的名称.
Console.WriteLine("The name of the channel is {0}.",
serverChannel.ChannelName);
// 打印这个信道的优先级.
Console.WriteLine("The priority of the channel is {0}.",
serverChannel.ChannelPriority);
// 打印这个信道的URI数组.
System.Runtime.Remoting.Channels.ChannelDataStore channelData =(System.Runtime.Remoting.Channels.ChannelDataStore) serverChannel.ChannelData;
foreach (string uri in channelData.ChannelUris)
...{
Console.WriteLine("The channel URI is {0}.", uri);
}
// 向信道暴露一个远程对象.
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", System.Runtime.Remoting.WellKnownObjectMode.Singleton);
Console.WriteLine("Press ENTER to exit the server.");
Console.ReadLine();
Console.WriteLine("The server is exiting.");
}
}
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Server
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道
IpcChannel serverChannel = new IpcChannel("TestChannel");
// 注册这个IPC信道.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(serverChannel);
// 打印这个信道的名称.
Console.WriteLine("The name of the channel is {0}.",
serverChannel.ChannelName);
// 打印这个信道的优先级.
Console.WriteLine("The priority of the channel is {0}.",
serverChannel.ChannelPriority);
// 打印这个信道的URI数组.
System.Runtime.Remoting.Channels.ChannelDataStore channelData =(System.Runtime.Remoting.Channels.ChannelDataStore) serverChannel.ChannelData;
foreach (string uri in channelData.ChannelUris)
...{
Console.WriteLine("The channel URI is {0}.", uri);
}
// 向信道暴露一个远程对象.
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", System.Runtime.Remoting.WellKnownObjectMode.Singleton);
Console.WriteLine("Press ENTER to exit the server.");
Console.ReadLine();
Console.WriteLine("The server is exiting.");
}
}
客户端代码:
using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Client
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道。
IpcChannel channel = new IpcChannel();
// 注册这个信道。
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);
// 注册一个远程对象的客户端代理.
System.Runtime.Remoting.WellKnownClientTypeEntry remoteType = new System.Runtime.Remoting.WellKnownClientTypeEntry(typeof(RemoteObject),"ipc://TestChannel/RemoteObject.rem");
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);
RemoteObject service = new RemoteObject();
Console.WriteLine("The client is invoking the remote object.");
Console.WriteLine("The remote object has been called {0} times.",
service.GetCount());
}
}
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Client
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道。
IpcChannel channel = new IpcChannel();
// 注册这个信道。
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);
// 注册一个远程对象的客户端代理.
System.Runtime.Remoting.WellKnownClientTypeEntry remoteType = new System.Runtime.Remoting.WellKnownClientTypeEntry(typeof(RemoteObject),"ipc://TestChannel/RemoteObject.rem");
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);
RemoteObject service = new RemoteObject();
Console.WriteLine("The client is invoking the remote object.");
Console.WriteLine("The remote object has been called {0} times.",
service.GetCount());
}
}
主要代码就算完成了。但,还有一个问题,那就是如果服务端和客户端在不同的Windows帐户运行的时候,会有验证权限的问题。对于这个问题,我们只要把服务端的信道注册代码改一下就好了:
Hashtable ht = new Hashtable();
ht["portName"] = "TestChannel";
ht["name"] = "ipc";
ht["authorizedGroup"] = "Everyone";
serverChannel= new IpcChannel(ht, null, provider);
ht["portName"] = "TestChannel";
ht["name"] = "ipc";
ht["authorizedGroup"] = "Everyone";
serverChannel= new IpcChannel(ht, null, provider);
- 让.NET Remoting更快些-IPCChannel的实现
- 让.NET Remoting更快些-IPCChannel的实现
- 让.NET Remoting更快些-IPCChannel的实现
- 让makefile飞的更快些
- 如何才能让钱来得更快些
- 缩短页面的渲染时间 让页面跑的更快些
- 怎么才能使蜘蛛爬的更快些
- 。net 的remoting 技术
- 如何让程序跑得更快些?——试试Visual Studio中的性能分析工具 (By Jun Guo)
- 如何让程序跑得更快些?——试试Visual Studio中的性能分析工具 (By Jun Guo)
- 让INotifyPropertyChanged的实现更优雅一些
- 利用.net Remoting 实现文件传输
- .Net Remoting安全性与实现
- .Net Remoting 实现简易的"命令行控制台"聊天室
- 使用Fluorine实现Flash与.NET之间的Remoting通信
- 用WebORB实现flex + .net后台的Remoting
- .Net基于Remoting远程调用实现的AOP拦截
- 可以这样让pod update快些
- Zend系列产品的php.ini配置
- 一些验证方面的资料
- 通用进度计数器
- 一个javascript脚本库
- 跟“无为”学习Ajax技术第八天
- 让.NET Remoting更快些-IPCChannel的实现
- IE和FireFox中层的拖动详解。
- AspNetPager分页控件
- Johnny Deng 语录 1
- ASP.NET中导出excel/word等类型文件的简单方法
- 得到select中options的值,隐藏div的js代码
- eos
- RGBvsYUV
- .NET常用代码