Sniper

犯强汉者,虽远必诛!

博客园 首页 新随笔 联系 订阅 管理

 在例子一中,会存在3个问题:
1.创建通道,对象注册和其他配置代码过多,如果这些细节改变,则需要重新编译
2.对于已知(Well-Known)对象,只能通过默认的构造函数来创建对象,客户端不能指定非默认的构造函数
3.客户端必须引用包含已知类型的程序集,因此需要把这个程序集部署到每个客户端上,违背了分布式的原则

本节将解决问题1

步骤1:服务器端
写MathServer.exe.config配置文件:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
    
<System.runtime.remoting>
        
<application>
                <!--Service中可以指定任意数目的WellKnown元素,mode指定服务形式,type指定将提供的类型,格式是:类型名,库名;objectUri指定分配给此对象的已知名称-->
            
<service>
                
<wellknown mode="Singleton" type="MathLibrary.SimpleMath,MathLibrary" objectUri="MyURI.soap"/>
            
</service>
            
<channels>
                    <!--可以指定多个通道,此处的ref是引用Machine.config文件中的通道名称-->
                
<channel port="13101" ref="http"/>
            
</channels>
        
</application>
    
</System.runtime.remoting>
</configuration>
对应的服务器端代码ServerMain.cs:
 1using System;
 2using System.Runtime.Remoting;
 3using System.Runtime.Remoting.Channels;
 4using System.Runtime.Remoting.Channels.Http;
 5namespace MathServer
 6{
 7    /// <summary>
 8    /// ServerMain 的摘要说明。
 9    /// </summary>

10    public class ServerMain
11    {
12        public ServerMain()
13        {
14            //
15            // TODO: 在此处添加构造函数逻辑
16            //
17        }

18        public static void Main(string[] args)
19        {
20            RemotingConfiguration.Configure("MathServer.exe.config");
21            Console.WriteLine("Server started.Press Enter to End..");
22            Console.ReadLine();
23        }

24    }

25}
客户端配置文件MathClient.exe.Config: 
<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
    
<System.runtime.remoting>
        
<application>
            
<client displayName="MathClient">
                
<wellknown type="MathLibrary.SimpleMath,MathLibrary" url="http://localhost:13101/MyURI.soap"/>
            
</client>
            
<channels>
                
<channel type="System.Runtime.Remoting.Channels.Http.HttpChannel,System.Runtime.Remoting,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b77a5c451934e089"/>
            
</channels>
        
</application>
    
</System.runtime.remoting>
</configuration>
对应的客户端代码如下:
 1using System;
 2using System.Runtime.Remoting;
 3using System.Runtime.Remoting.Channels;
 4using System.Runtime.Remoting.Channels.Http;
 5using MathLibrary;
 6namespace 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            RemotingConfiguration.Configure("MathClient.exe.config");
22            SimpleMath math4 = new SimpleMath();
23
24            Console.WriteLine("5+2={0}",math4.Add(5,2));
25            Console.WriteLine("5-2={0}",math4.Subtract(5,2));
26            Console.WriteLine("Press enter to End");
27            Console.ReadLine();
28        }

29    }

30}
posted on 2005-08-14 11:54  Sniper  阅读(566)  评论(0)    收藏  举报