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

前面的笔记也谈到了这个问题,但没有例子来说明一下,所以今天把webcast的例子给大家作为例子演示一下他们的不同:
类库项目中包含两个文件: class1.cs和 helloserver.cs ,代码如下:
class1.cs
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  RemotingSamples
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif       [Serializable]
InBlock.gif    
public class MySerialized 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MySerialized(int val)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            a 
= val;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Foo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"MySerialized.Foo called");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int A
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"MySerialized.A called");
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                a 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected int a;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class MyRemote : System.MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyRemote(int val)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            a 
= val;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
~MyRemote()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"MyRemote destructor");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Foo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"MyRemote.Foo called");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int A
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"MyRemote.A called");
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                a 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected int a;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
可以看到有两个类一个是MySerialized 前面有[Serializable]所以它是可序列化的,可以按值列集,另一个是MyRemote继承自System.MarshalByRefObject,可以按引用列集.
helloserver.cs
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  RemotingSamples
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HelloServer : MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public HelloServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"HelloServer activated");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public String HelloMethod(String name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
InBlock.gif                
"Server Hello.HelloMethod : {0}", name);
InBlock.gif            
return "Hi there " + name;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MySerialized GetMySerialized()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new MySerialized(4711);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public MyRemote GetMyRemote()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new MyRemote(4712);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
还有server和client两个控制台应用程序
server.cs
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
public class Server
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static int Main(string [] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            TcpChannel chan1 
= new TcpChannel(8085);
InBlock.gif            HttpChannel chan2 
= new HttpChannel(8086);
InBlock.gif
InBlock.gif            ChannelServices.RegisterChannel(chan1);
InBlock.gif            ChannelServices.RegisterChannel(chan2);
InBlock.gif
InBlock.gif
InBlock.gif            RemotingConfiguration.RegisterWellKnownServiceType
InBlock.gif                (
InBlock.gif                
typeof(HelloServer),
InBlock.gif                
"SayHello",
InBlock.gif                WellKnownObjectMode.SingleCall
InBlock.gif                );
InBlock.gif            
InBlock.gif
InBlock.gif            System.Console.WriteLine(
"Press Enter key to exit");
InBlock.gif            System.Console.ReadLine();
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
client.cs
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
using  System.IO;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Client
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//使用TCP通道得到远程对象
InBlock.gif
            TcpChannel chan1 = new TcpChannel();
InBlock.gif            ChannelServices.RegisterChannel(chan1);
InBlock.gif            HelloServer obj1 
= (HelloServer)Activator.GetObject(
InBlock.gif                
typeof(RemotingSamples.HelloServer),
InBlock.gif                
"tcp://localhost:8085/SayHello");
InBlock.gif            
if (obj1 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.WriteLine(
InBlock.gif                    
"Could not locate TCP server");
ExpandedSubBlockEnd.gif            }

InBlock.gif            MySerialized ser 
= obj1.GetMySerialized();
InBlock.gif            
if (!RemotingServices.IsTransparentProxy(ser))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"ser is not a transparent proxy");
ExpandedSubBlockEnd.gif            }

InBlock.gif            ser.Foo();
InBlock.gif            MyRemote rem 
= obj1.GetMyRemote();
InBlock.gif            
if (RemotingServices.IsTransparentProxy(rem))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"ser is a transparent proxy");
ExpandedSubBlockEnd.gif            }

InBlock.gif            rem.Foo();
InBlock.gif
InBlock.gif            System.Console.ReadLine();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

运行后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出现在客户端,并未在服务器端出现,它为按值列集,在本地得到远程对象的副本,直接在本地访问副本.

 


 

转载于:https://www.cnblogs.com/stuhrbeu/archive/2007/02/04/639713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值