Webcast.NET Remoting学习笔记(4)按值列集和按引用列集

前面的笔记也谈到了这个问题,但没有例子来说明一下,所以今天把webcast的例子给大家作为例子演示一下他们的不同:
类库项目中包含两个文件: class1.cs和 helloserver.cs ,代码如下:
class1.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingSamples
{
       [Serializable]
    
public class MySerialized 
    
{
        
public MySerialized(int val)
        
{
            a 
= val;
        }

        
public void Foo()
        
{
            Console.WriteLine(
"MySerialized.Foo called");
        }

        
public int A
        
{
            
get
            
{
                Console.WriteLine(
"MySerialized.A called");
                
return a;
            }

            
set
            
{
                a 
= value;
            }

        }

        
protected int a;
    }

    
public class MyRemote : System.MarshalByRefObject
    
{
        
public MyRemote(int val)
        
{
            a 
= val;
        }

        
~MyRemote()
        
{
            Console.WriteLine(
"MyRemote destructor");
        }

        
public void Foo()
        
{
            Console.WriteLine(
"MyRemote.Foo called");
        }

        
public int A
        
{
            
get
            
{
                Console.WriteLine(
"MyRemote.A called");
                
return a;
            }

            
set
            
{
                a 
= value;
            }

        }

        
protected int a;
    }

}
可以看到有两个类一个是MySerialized 前面有[Serializable]所以它是可序列化的,可以按值列集,另一个是MyRemote继承自System.MarshalByRefObject,可以按引用列集.
helloserver.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingSamples
{
    
public class HelloServer : MarshalByRefObject
    
{
        
public HelloServer()
        
{
            Console.WriteLine(
"HelloServer activated");
        }

        
public String HelloMethod(String name)
        
{
            Console.WriteLine(
                
"Server Hello.HelloMethod : {0}", name);
            
return "Hi there " + name;
        }

        
public MySerialized GetMySerialized()
        
{
            
return new MySerialized(4711);
        }


        
public MyRemote GetMyRemote()
        
{
            
return new MyRemote(4712);
        }

    }

}
还有server和client两个控制台应用程序
server.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace RemotingSamples 
{

    
public class Server
    
{
        
public static int Main(string [] args) 
        
{

            TcpChannel chan1 
= new TcpChannel(8085);
            HttpChannel chan2 
= new HttpChannel(8086);

            ChannelServices.RegisterChannel(chan1);
            ChannelServices.RegisterChannel(chan2);


            RemotingConfiguration.RegisterWellKnownServiceType
                (
                
typeof(HelloServer),
                
"SayHello",
                WellKnownObjectMode.SingleCall
                );
            

            System.Console.WriteLine(
"Press Enter key to exit");
            System.Console.ReadLine();
            
return 0;
        }


    }

}
client.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;

namespace RemotingSamples 
{
    
public class Client
    
{
        
public static void Main(string[] args)
        
{
            
//使用TCP通道得到远程对象
            TcpChannel chan1 = new TcpChannel();
            ChannelServices.RegisterChannel(chan1);
            HelloServer obj1 
= (HelloServer)Activator.GetObject(
                
typeof(RemotingSamples.HelloServer),
                
"tcp://localhost:8085/SayHello");
            
if (obj1 == null)
            
{
                System.Console.WriteLine(
                    
"Could not locate TCP server");
            }

            MySerialized ser 
= obj1.GetMySerialized();
            
if (!RemotingServices.IsTransparentProxy(ser))
            
{
                Console.WriteLine(
"ser is not a transparent proxy");
            }

            ser.Foo();
            MyRemote rem 
= obj1.GetMyRemote();
            
if (RemotingServices.IsTransparentProxy(rem))
            
{
                Console.WriteLine(
"ser is a transparent proxy");
            }

            rem.Foo();

            System.Console.ReadLine();

        }


    }

}

运行后server端显示
Press Enter key to exit
HelloServer activated
HelloServer activated
MyRemote.Foo called
client显示
ser is not a transparent proxy
MySerialized.Foo called
ser is a transparent proxy
可见MyRemote为按引用列集,MyRemote.Foo called出现在server端,并且client端显示ser is a transparent proxy,它是通过在本地创建代理来访问远程对象.MySerialized.Foo called出现在客户端,并未在服务器端出现,它为按值列集,在本地得到远程对象的副本,直接在本地访问副本.

 


 

posted on 2007-02-04 18:43  stuhrbeu  阅读(342)  评论(0编辑  收藏  举报

导航