WCF分布式开发必备知识系列文章2:Net Remoting(2)
namespace NetRemotingHost
2{
3 class NetRemotingHost
4 {
5 //创建宿主应用程序
6 static void Main(string[] args)
7 {
8
9 /**///////////////////////////////////////创建三种通道/////////////////////////////////////////////////
10 ///创建Tcp通道,使用端口10001
11 TcpChannel chanTcp = new TcpChannel(10001);
12 /**////创建Http通道,使用端口10002
13 HttpChannel chanHttp = new HttpChannel(10002);
14 /**////创建IPC通道,使用端口10003,IPC只适合同系统内进程的通信,所以不需要设置端口和主机名
15 IpcChannel chanIPC = new IpcChannel("FrankTestIpc");
16 /**///////////////////////////////////////注册通道//////////////////////////////////////////////////
17
18 ///注册TCP通道
19 ChannelServices.RegisterChannel(chanTcp);
20 /**////注册HTTP通道
21 ChannelServices.RegisterChannel(chanHttp);
22 /**////注册IPC通道
23 ChannelServices.RegisterChannel(chanIPC);
24
25 /**/////////////////////////////////////////打印通道/////////////////////////////////////////////////
26 // 打印TCP通道的名称.
27 Console.WriteLine("The name of the TCPChannel is {0}.",
28 chanTcp.ChannelName);
29 // 打印TCP通道的优先级.
30 Console.WriteLine("The priority of the TCPChannel is {0}.",
31 chanTcp.ChannelPriority);
32
33 // 打印Http通道的名称.
34 Console.WriteLine("The name of the HttpChannel is {0}.",
35 chanTcp.ChannelName);
36 // 打印Http通道的优先级.
37 Console.WriteLine("The priority of the HttpChannel is {0}.",
38 chanTcp.ChannelPriority);
39
40 // 打印IPC通道的名称.
41 Console.WriteLine("The name of the IpcChannel is {0}.",
42 chanTcp.ChannelName);
43 // 打印IPC通道的优先级.
44 Console.WriteLine("The priority of the IpcChannel is {0}.",
45 chanTcp.ChannelPriority);
46 /**////////////////////////////////////////////注册对象/////////////////////////////////////////////////
47 //注册对象MyRemoteObject到Net Remoting运行库
48 //配置远程通信基础框架.第2个参数是对象的URI,“RemoteObject.MyRemoteObject”,客户端URL一定要和这个匹配,不然会出现查找不到代理对象的异常
49 RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.MyRemoteObject), "RemoteObject.MyRemoteObject", WellKnownObjectMode.Singleton);
50 //远程对象激活方式是单件激活模式,每次调用共享一个对象,SingleCall激活模式会在每次调用的时候产生一个新对象
51
52 //向信道暴露一个IPC远程对象.
53 //RemotingConfiguration.RegisterWellKnownService(Type(typeof(RemoteObject), "RemoteObject.rem", System.Runtime.Remoting.WellKnownObjectMode.Singleton);
54
55 /**////////////////////For Debug/////////////////////////////////////////////////////////////////////////
56 Console.WriteLine("Press any key to exit!");
57 System.Console.ReadLine();
58
59 }
60
61
62 }
63}
64
创建Tcp通道,使用端口10001, 创建Http通道,使用端口10002,创建IPC通道,使用端口10003,IPC只适合同系统内进程的通信,所以不需要设置端口和主机名.然后调用ChannelServices类的静态方法RegisterChannel进行注册.最后一步注册对象MyRemoteObject到Net Remoting运行库,同时要先配置远程通信基础框架.第2个参数是对象的URI,“RemoteObject.MyRemoteObject”,客户端URL一定要和这个匹配,不然会出现查找不到代理对象的异常.WellKnownObjectMode.Singleton);远程对象激活方式是单件激活模式,每次调用共享一个对象,SingleCall激活模式会在每次调用的时候产生一个新对象.