WCF分布式开发必备知识系列文章2:Net Remoting(3)
3客户端是控制台程序(实际项目类型可以替换,这里只是为了作为例子选择控制台类型).配置文件进行的设置如下:
Code
1<configuration>
2 <appSettings>
3 <add key="ServiceURLTcp" value="tcp://localhost:10001/RemoteObject.MyRemoteObject"/>
4 <add key="ServiceURLHttp" value="http://localhost:10002/RemoteObject.MyRemoteObject"/>
5 <add key="ServiceURLIpc" value="ipc://FrankTestIpc/RemoteObject.MyRemoteObject"/>
6 </appSettings>
7 <system.runtime.remoting>
8
9 </system.runtime.remoting>
10</configuration>
配置文件设置的是具体通道的URL信息.具体c#实现代码如下:
namespace RemoteClient
3{
4 class MyClient//客户端
5 {
6 [STAThread]//主线程,建立客户端程序:注册通道,根据URL得到对象代理,使用代理调用远程对象
7
8 static void Main(string[] args)
9 {
10 //为远程对象创建代理
11 //代理的优势在于不仅可以跨域访问对象还可以跨进程,和系统,使用TCP通道
12 try
13 {
14 /**/////
15 RemoteObject.MyRemoteObject proxyObjectTcp = (RemoteObject.MyRemoteObject)Activator.GetObject(typeof(RemoteObject.MyRemoteObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURLTcp"]);
16 //通过代理访问对象的方法,输出结果
17 Console.WriteLine("This call object by TcpChannel,100+200 = {0}", proxyObjectTcp.AddForTcpTest(100, 200));
18
19 /**///////
20 RemoteObject.MyRemoteObject proxyObjectHttp = (RemoteObject.MyRemoteObject)Activator.GetObject(typeof(RemoteObject.MyRemoteObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURLHttp"]);
21 //通过代理访问对象的方法,输出结果
22 Console.WriteLine("This call object by HttpChannel,100-200 = {0}", proxyObjectHttp.MinusForHttpTest(100, 200));
23
24 /**///// 注册一个远程对象的客户端代理.
25 //System.Runtime.Remoting.WellKnownClientTypeEntry remoteType = new System.Runtime.Remoting.WellKnownClientTypeEntry(typeof(RemoteObject.MyRemoteObject), "ipc://FrankTestIpc/RemoteObject.MyRemoteObject");
26 //System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);//如果需要客户端和住进程通讯,要在客户端注册代理,方式和服务器端注册注册远程对象的代理相同
27
28 RemoteObject.MyRemoteObject proxyObjectIpc = (RemoteObject.MyRemoteObject)Activator.GetObject(typeof(RemoteObject.MyRemoteObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURLIpc"]);
29 //通过代理访问对象的方法,输出结果
30 Console.WriteLine("This call object by IpcChannel,100*200 = {0}", proxyObjectIpc.MultipleForIpcTest(100, 200));
31 }
32 catch (Exception e)
33 {
34 throw e;
35 }
36 finally
37 {
38
39 }
40 //For Debug
41 Console.WriteLine("Press any key to exit!");
42 Console.ReadLine();
43 }
44
45 }
46}
主进程通过配置获取远程对象的信息,为远程对象创建代理,代理的优势在于不仅可以跨域访问对象还可以跨进程,和系统,使用TCP通道,降低系统耦合性.最后客户端通过代理访问远程对象的方法,输出结果.首先要运行服务器端,其次是客户端,IDE使用的是Visual Studio 2005/2008.其次注意项目引用.运行后的结果显示如下.
服务器:
显示3个通道都注册成功.
客户端:
客户端分别通过3种方式调用远程对象,进行运算.测试成功!
本文代码下载./Files/frank_xl/NetRemoting.rar
小结:以上就是全部的.Net Remoting的实现过程.当然.Net Remoting知识范围很广,还有异步调用,安全等.以后再做深入的学习.接下来一节我打算写关于Enterprise Services的文章,其中会涉及到COM+的知识,如COM+中事务机制.我们有必要好好学习一下.希望本文的能给大家在WCF学习上对.Net Remoting技术的理解有所帮助.大家有问题可以一起交流~
WCF分布式开发必备知识系列文章快速学习列表:
WCF分布式开发必备知识系列文章1:MSMQ消息队列
http://www.xueit.com/html/2009-02/21_625_00.html
WCF分布式开发必备知识系列文章2:Net Remoting
http://www.xueit.com/html/2009-02/21_626_00.html
WCF分布式开发必备知识系列文章3:Enterpise Services
http://www.xueit.com/html/2009-02/21_627_00.html
WCF分布式开发必备知识系列文章4:Web Service
http://www.xueit.com/html/2009-02/21_628_00.html