Sniper

犯强汉者,虽远必诛!

博客园 首页 新随笔 联系 订阅 管理
实现已知对象:
1.构建服务器端:
(1)添加对System.Runtime.Remoting.dll
(2)实现一个派生自MarshalByRefObject的类
(3)选择一种可用的通道,TCP,HTTP,然后用ChannelServices.RegisterChannel()方法注册此通道
(4)用RemotingConfiguration.RegisterWellKnownServiceType()方法把这个类注册为已知对象
(5)保持服务器处于激活态
首先是继承了MarshalByRefObject的类:

using System;

namespace
 MathLibrary
{
    
/// <summary>
    
///
 SimpleMath 的摘要说明。
    
/// </summary>

    public class
 SimpleMath:MarshalByRefObject
    
{
        
public
 SimpleMath()
        
{
            
//

            
//
 TODO: 在此处添加构造函数逻辑
            
//
        }

        
public int Add(int a ,int
 b)
        
{
            Console.WriteLine(
"SimpleMath.Add({0},{1})"
,a,b);
            
return a+
b;
        }

        
public int Subtract(int a, int
 b)
        
{
            Console.WriteLine(
"SimpleMath.Subtract({0},{1})"
,a,b);
            
return a-
b;
        }

    }

}

接着构建服务器端:

 1using System;
 2using
 System.Runtime.Remoting;
 3using
 System.Runtime.Remoting.Channels;
 4using
 System.Runtime.Remoting.Channels.Http;
 5using
 MathLibrary;
 6

 7namespace
 MathServer
 8
{
 9    
/// <summary>
10    ///
 ServerMain 的摘要说明。
11    /// </summary>

12    public class
 ServerMain
13    
{
14        public
 ServerMain()
15        
{
16            //

17            //
 TODO: 在此处添加构造函数逻辑
18            
//
19        }

20        public static void Main(string
[] args)
21        
{
22            //建立一个通道并指定段口号

23            HttpChannel channel = new HttpChannel(13101
);
24            //通过运行时的远程服务来注册通道

25
            ChannelServices.RegisterChannel(channel);
26            //注册被调用类为已知对象well-know

27
            RemotingConfiguration.RegisterWellKnownServiceType(
28                typeof(MathLibrary.SimpleMath),//被注册的类

29                "MyURI.soap",                //已知对象名称Well-Know Name

30                WellKnownObjectMode.Singleton);    //被调用模式Singleton 或者 SingleCall

31
            
32            Console.WriteLine("Server started.Press Enter to End!"
);
33
            Console.ReadLine();            
34        }

35    }

36}


然后构建客户端,注意代码注释:
需要知道如下信息:服务器机器名,通道类型,端口,分配给远程对象的URI
(1)添加引用如下所示
(2)添加对程序集的引用,本例中为MathLibrary.dll
(3)使用与服务器相同的通道类型注册一个通道,本例中为HTTP
(4)调用Activator.GetObject()方法,传递合适的URL以取得一个远程对象的代理
(5)找代理强制转换成合适的类型,并把他当真实对象使用

 1 using System;
 2 using System.Runtime.Remoting;
 3 using System.Runtime.Remoting.Channels;
 4 using System.Runtime.Remoting.Channels.Http;
 5 using MathLibrary;
 6 namespace MathClient
 7 {
 8     /// <summary>
 9     /// ClientMain 的摘要说明。
10     /// </summary>
11     public class ClientMain
12     {
13         public ClientMain()
14         {
15             //
16             // TODO: 在此处添加构造函数逻辑
17             //
18         }
19         public static void Main(string[] args)
20         {
21             //创建并注册通道,没有指定段口号,所以不能接受消息
22             HttpChannel channel = new HttpChannel();
23             ChannelServices.RegisterChannel(channel);
24 
25             //获取远程对象的一个代理
26             Object remoteObj = Activator.GetObject(typeof(MathLibrary.SimpleMath),"Http://localhost:13101/MyURI.soap");
27             //将代理转换为所需要的类
28             SimpleMath math1 = (SimpleMath)remoteObj;
29 
30             //方法2
31             SimpleMath math2 = (SimpleMath)RemotingServices.Connect(
32                 typeof(MathLibrary.SimpleMath),
33                 "Http://localhost:13101/MyURI.soap"
34                 );
35             //方法3
36             RemotingConfiguration.RegisterWellKnownClientType(
37                 typeof(MathLibrary.SimpleMath),
38                 "Http://localhost:13101/MyURI.soap"
39                 );
40             SimpleMath math3 = new SimpleMath();
41             
42             
43 
44             Console.WriteLine("5+2={0}",math3.Add(5,2));
45             Console.WriteLine("5-2={0}",math3.Subtract(5,2));
46             Console.WriteLine("Press enter to End");
47             Console.ReadLine();
48         }
49     }
50 }
posted on 2005-08-14 00:01  Sniper  阅读(380)  评论(0)    收藏  举报