IPCChannel是.NET Framework 2.0 裡面新增的,它使用 Windows 處理序間通訊 (IPC) 系統在同一電腦上的應用程式定義域之間傳輸訊息。在同一電腦上的應用程式定義域之間進行通訊時,IPC 通道比 TCP 或 HTTP 通道要快得多。但是IPC只在本機應用之間通訊。所以,在用戶端和服務端在同一台機器時,我們可以通過註冊IPCChannel來提高Remoting的效能。但如果用戶端和服務端不在同一台機器時,我們不能註冊IPCChannel。
IPC(Inter-Process Communication,處理序間通訊)
/Files/wucg/_TestProjects/TestNetRemoting_IPC通訊.rar
eg1:
server端
namespace DotNetRemotingDemo2Server
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("DotNetRemotingDemo2Server.exe.config",false);
Console.ReadLine();
}
}
}
配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<!--<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="Singleton" />-->
<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="SingleCall" />
</service>
<channels>
<channel ref="ipc" portName="testPipe" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client端:
namespace DotNetRemotingDemo2Client
{
class Program
{
static void Main(string[] args)
{
RemoteHelloConsole.Hello hello =
(RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"ipc://testPipe/Object.MyObject");
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(hello.Greeting("abc123" + i));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
eg2:
Server端:
namespace DotNetRemotingDemo1
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
//typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.SingleCall);
typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.Singleton);
Console.WriteLine("press return to exit.");
Console.ReadLine();
}
}
}
Client端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace DotNetRemotingClient
{
class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
RemoteHelloConsole.Hello hello = (RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"tcp://localhost:8086/Hi");
if (hello == null) {
Console.WriteLine("can't locate server.");
return;
}
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine( hello.Greeting("abc123" + i) );
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}