关于.Net Remoting 的一个简单示例

我对.Net Remoting 是从2002年就已经知道这个说法了。但是一直很害怕这个怪物。因为每次想试图去搞清楚它的时候,
一看他的那么多页的文档,我是看不下去了.因为他总是先告诉你很多恐怖的概念,中间夹杂一些"断章取义"的代码,从来就
没有一个完整的代码. 所以总是这样虎头蛇尾的.估计大家也有很多人是这样子的. 今天我不跟大家说概念了.  .Net Remoting 整个框架分成三部分:
1. 服务器端
  需要引用2
  代码如下:  
 

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  HelloServer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Class1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Server
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpServerChannel tcpChannel 
= new HttpServerChannel(5256);
InBlock.gif            ChannelServices.RegisterChannel(tcpChannel);
InBlock.gif            
//if you set WellKnownObjectMode.SingleCall,when client call, server side will create a new object even if there is a old object.
InBlock.gif
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello.Hello),"Shark",WellKnownObjectMode.SingleCall);
InBlock.gif            System.Console.WriteLine(
"Hit to exit");
InBlock.gif            System.Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif

 
2. 具体实现服务的类库
  代码如下:
 

None.gif using  System;
None.gif
using  System.Data;
None.gif
namespace  Hello
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif    
public class Hello:System.MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Hello()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Constructor");
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        
public string Greeting(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Greeting");
InBlock.gif            
return "Hello," + name;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataTable GetData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            dt.Columns.Add(
new DataColumn("ID",typeof(String)));
InBlock.gif            DataRow rs 
= dt.NewRow();
InBlock.gif            rs[
"ID"= "1";
InBlock.gif            dt.Rows.Add(rs);
InBlock.gif            rs 
= dt.NewRow();
InBlock.gif            rs[
"ID"= "2";
InBlock.gif            dt.Rows.Add(rs);
InBlock.gif            rs 
= dt.NewRow();
InBlock.gif            rs[
"ID"= "3";
InBlock.gif            dt.Rows.Add(rs);
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


2. 客户端
  需要引用2
  代码如下:
  

None.gif private   void  button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//You can use the following method to create remoting object
InBlock.gif            
//Method 1:Hello.Hello obj = (Hello.Hello)Activator.GetObject(typeof(Hello.Hello),"http://127.0.0.1:5256/Shark");
InBlock.gif            
//Method 2:Hello.Hello obj = (Hello.Hello)RemotingServices.Connect(typeof(Hello.Hello),"http://127.0.0.1:5256/Shark");
ContractedSubBlock.gifExpandedSubBlockStart.gif
            Method 3:#region Method 3:
InBlock.gif            RemotingConfiguration.RegisterWellKnownClientType(
typeof(Hello.Hello),"http://127.0.0.1:5256/Shark");
InBlock.gif            Hello.Hello obj 
= new Hello.Hello();
ExpandedSubBlockEnd.gif            
#endregion
 
InBlock.gif            
ContractedSubBlock.gifExpandedSubBlockStart.gif            
Method 4:#region Method 4:
InBlock.gif            
//object []attrs = {new UrlAttribute("http://127.0.0.1:5256/Shark")};
InBlock.gif            
//ObjectHandle handle = Activator.CreateInstance("Hello","Hello.Hello",attrs);
InBlock.gif            
//Hello.Hello obj =  (Hello.Hello)handle.Unwrap();
ExpandedSubBlockEnd.gif
            #endregion
 
InBlock.gif            
if(obj != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.dataGrid1.DataSource = obj.GetData();
InBlock.gif                
this.Text = obj.Greeting(System.DateTime.Now.ToString());
InBlock.gif                ILease lease 
= (ILease)obj.GetLifetimeService();
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  Form1_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ChannelServices.RegisterChannel(
new HttpClientChannel());
ExpandedBlockEnd.gif        }


整个例子的完整代码,请下载http://www.cnblogs.com/Files/SharkXu/DotNetRemoting.zip.

转载于:https://www.cnblogs.com/SharkXu/archive/2006/08/07/NetRemoting.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值