Remoting学习三

       通过前面所讲,实现Remoting大致有这么几个步骤。

       1.先定义一个远程对象:

View Code
1 namespace ServerAssembly
2 {
3 public class DemoClass:MarshalByRefObject
4 {
5 private int count = 0;
6 public DemoClass()
7 {
8 Console.WriteLine("-----------DemoClass的构造函数被调用-----------");
9 }
10
11 /// <summary>
12 /// 显示输出的次数
13 /// </summary>
14 /// <param name="name"></param>
15 public void ShowCount(string name)
16 {
17 count++;
18 Console.WriteLine("{0},the count is{1}",name,count);
19 }
20
21 /// <summary>
22 /// 显示当前应用程序域
23 /// </summary>
24 public void ShowAppDomain()
25 {
26 AppDomain appDomain = AppDomain.CurrentDomain;
27 Console.WriteLine("Current Domain is {0}",appDomain.FriendlyName);
28 }
29
30 public int GetCount()
31 {
32 return this.count;
33 }
34 }
35 }

        2.接下来注册通道。

View Code
1 public static void RegisterChannel()
2 {
3 IChannel tcpChannel = new TcpChannel(8051);
4 ChannelServices.RegisterChannel(tcpChannel, false);
5
6 IChannel httpChannel = new HttpChannel(8052);
7 ChannelServices.RegisterChannel(httpChannel, false);
8 }

       3.接下来注册服务器端对象,确定是要以什么形式来发布服务器端对象,客户端激活,服务器端Singleton激活,服务器端SingleCall激活。

View Code
1 public static void ClientActivated()
2 {
3 Console.WriteLine("方式: 客户端激活方式");
4 Type t = typeof(DemoClass);
5 RemotingConfiguration.RegisterActivatedServiceType(t);
6
7 }
8
9 private static void ServerActivatedSingleCall()
10 {
11 Console.WriteLine("方式: 服务器端激活 SingleCall");
12 Type t = typeof(DemoClass);
13 RemotingConfiguration.RegisterWellKnownServiceType(
14 t, "ServerActivated", WellKnownObjectMode.SingleCall);
15 }
16
17 private static void ServerActivatedSingleton()
18 {
19 Console.WriteLine("方式: 服务端激活 Singleton");
20 Type t = typeof(DemoClass);
21 RemotingConfiguration.RegisterWellKnownServiceType(
22 t, "ServerActivated", WellKnownObjectMode.Singleton);
23 }

        4.对于客户端要做的事情就是引用远程对象所在的程序集。接下来在客户端也要注册远程对象。对于在客户端注册时,注册为服务器端激活时只有一种选择方法。

View Code
1 static void ClientActivated()
2 {
3 Type type = typeof(DemoClass);
4 string url = "tcp://127.0.0.1:8051";
5 RemotingConfiguration.RegisterActivatedClientType(type, url);
6
7 }
8
9 static void ServerActivated()
10 {
11 Type type = typeof(DemoClass);
12 string url = "tcp://127.0.0.1:8051/ServerActivated";
13 RemotingConfiguration.RegisterWellKnownClientType(type, url);
14
15 }

      接下来添加测试方法,在服务器端和客户端分别用客户端激活,服务器端SingleCall激活,服务器端Singleton激活。注意如果客户端采用客户端激活,而服务器端采用服务器端激活则会跑出异常,也就是客户端和服务器端的激活方式要一致。最后分析运行结果。

1.采用客户端激活的方式

服务器端截图:

客户端截图:

结论:

(1).采用客户端激活对于客户端每个创建的远程对象,服务器端会一一对应创建每个对象,因此我们可以从图上看到调用了两次构造函数。

(2).对于远程对象方法的调用是在服务器端执行的。

(3).并且会在每个对象中保存状态,因为我们可以看到对于同一个对象第二次调用count的时候会输出2。

(4).使用客户激活方式时,远程对象在调用new操作时创建。(这一点可以通过修改RunTest来实现)

2.采用服务器端Singleton方式

服务器端截图:

客户端:

结论:

(1).对于客户端对远程对象的多次请求,只是创建一个服务器端对象供客户端使用。因此我们只看到构造函数被调用一次。

(2).对远程对象方法的调用都是在服务器端执行的。

(3).服务器端的远程对象会保存状态。count是被调用后一次累加的。

(4).对于采用Singleton方式,即使使用new操作符,客户端也无法创建一个对象,而只有在对象上第一次调用方法时才会创建。

3.服务器端SingleCall方式

服务器端截图:

 

客户端:

结论:

(1)我们可以看到在服务器端构造函数被调用了多大10次,这十次是这么来的。由于服务器端采用SingleCall调用方式,因此我们可以得知在客户端每次创建远程对象的代理时都会调用远程对象的构造函数。创建后马上销毁,也不保留状态。

(2)服务器端采用SingleCall调用方式,在客户端每次调用远程对象的方法的时候,也会创建对象,因此出现了另外的8个构造函数。

(3)调用远程对象的方法也是在服务器端执行。



(4).对于采用Singleton方式,即使使用new操作符,客户端也无法创建一个对象,而只有在对象上第一次调用方法时才会创建。

posted @ 2011-03-17 13:48  雁北飞  阅读(151)  评论(0编辑  收藏  举报