3.建立客户端程序:
注册通道
根据URL得到对象代理
使用代理调用远程对象
1using System;
2using System.Runtime.Remoting;
3using System.Runtime.Remoting.Channels;
4using System.Runtime.Remoting.Channels.Tcp;
5using System.Runtime.Remoting.Channels.Http;
6using System.IO;
7
8namespace SimpleRemoting
9{
10 public class Client
11 {
12 public static void Main(string[] args)
13 {
14 /**////使用TCP通道得到远程对象
15 TcpChannel chan1 = new TcpChannel();
16 ChannelServices.RegisterChannel(chan1);
17
18 HelloServer obj1 = (HelloServer)Activator.GetObject(
19 typeof(SimpleRemoting.HelloServer),
20 "tcp://localhost:8085/SayHello");
21
22 if (obj1 == null)
23 {
24 System.Console.WriteLine(
25 "连接TCP服务器失败");
26 }
27
28 /**////使用HTTP通道得到远程对象
29 HttpChannel chan2 = new HttpChannel();
30 ChannelServices.RegisterChannel(chan2);
31
32 HelloServer obj2 = (HelloServer)Activator.GetObject(
33 typeof(SimpleRemoting.HelloServer),
34 "http://localhost:8086/SayHello");
35
36 if (obj2 == null)
37 {
38 System.Console.WriteLine(
39 "连接HTTP服务器失败");
40 }
41
42 /**////输出信息
43 Console.WriteLine(
44 "ClientTCP HelloMethod {0}",
45 obj1.HelloMethod("Caveman1"));
46 Console.WriteLine(
47 "ClientHTTP HelloMethod {0}",
48 obj2.HelloMethod("Caveman2"));
49 Console.ReadLine();
50 }
51 }
52}
53
结束语:
初识用.NET Remoting来开发分布式应用就到这里了,有时间我会就.NET Remoting技术写成系列文章。包括基于租约的生存期,编组,异步远程调用等等。
