世俱杯 2025

Remoting编程知识(三)

原创|其它|编辑:郝浩|2009-07-08 09:33:32.000|阅读 323 次

概述:Remoting编程基本原理:当客户端创建远程RemotableClass的一个实例,.NET框架在客户端应用程序域中产生一个代理。该代理看起来就像实际对象。代理收到调用后,通过通道连接到远程的对象。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

程序范例:

Stopwatch.cs 
using System;
public class Stopwatch : MarshalByRefObject
{
DateTime mark = DateTime.Now;
public void Start ()
{
mark = DateTime.Now;
}
public int Stop ()
{
return (int) ((DateTime.Now - mark).TotalMilliseconds);
}
}
StopwatchServer.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp
{
static void Main ()
{
TcpServerChannel channel = new TcpServerChannel (1234);
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterActivatedServiceType
(typeof (Stopwatch));
Console.WriteLine ("Press Enter to terminate...");
Console.ReadLine ();
}
}
StopwatchClient.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp
{
static void Main ()
{
TcpClientChannel channel = new TcpClientChannel ();
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterActivatedClientType
(typeof (Stopwatch), "tcp://localhost:1234");
Stopwatch sw = new Stopwatch ();
sw.Start ();
Console.WriteLine ("Press Enter to show elapsed time...");
Console.ReadLine ();
Console.WriteLine (sw.Stop () + " millseconds");
}
}

五、Activator.GetObject和Activator.CreateInstance方法 
new操作符并不是激活远程对象的唯一方法。.NET框架提供了其他的激活方法:GetObject和CreateInstance。它们都是System.Activator类的成员。GetObject被用来激活在服务器端激活的对象,而CreateInstance被用来激活在客户端激活的对象。 
当使用GetObject或者CreateInstance来激活远程对象时,不再需要调用RegisterActivatedClientType或者RegisterWellKnownClientType来注册服务器上可远程化的类。例如:激活在服务器端激活的对象时: 
RemotingConfiguration.RegisterWellKnownClientType(typeof(Clock),”tcp://localhost:1234/Clock”);
Clock clock = new Clock(); 
可以使用下面的方法代:

Clock clock =(Clock) Activator.GetObject(typeof(Clock,”tcp://localhost:1234/Clock”);

激活客户端对象时: 
RemotingConfiguration.RegisterActivatedClientType(typeof(Stopwatch),”tcp://localhost:1234”);
Stopwatch sw = new StopWatch(); 
可以这样的方式:
object[] url ={new UrlAttribute(“tcp://localhost:1234”)}; 
Stopwatch sw =(Stopwatch) Activator.CreateInstance(typeof(Stopwatch),null,url); 
为什么要使用它们来代替new呢?因为在你仅知道URL和接口时,GetObject和CreateInstance可以仍使用。假设改变Clock类,它实现一个IClock接口。 
使用GetObject时: 

Iclock ic = (Iclock)Activator.GetObject(typeof(Iclock),”tcp://localhost:1234/Clock”); 
如果使用new,则会出现编译错误,因为new不能接受一个接口名称:
RemotingConfiguration.RegisterWellKnownClientType
(typeof (IClock), "tcp://localhost:1234/Clock");
IClock ic = new IClock ();

六、对象生存期和租用期 
一个single-call服务器端激活对象只在方法调用期间生存。之后,被垃圾回收器标记为删除。Singleton 服务器激活对象和客户端激活对象不一样,他们的生存期被租用控制。租用是一个对象,它实现了定义在System.Runtime.Remoting.Lifetime名称空间的Ilease接口。 
 Singleton 服务器端激活对象和客户端激活对象缺省的租用对象有一个5分钟的InitialLeaseTime,2分钟的RenewOnCallTime,5分钟的CurrentLeaseTime。如果对象没有方法被调用,当CurrentLeaseTime为0时它被清除,也就是5分钟后被清除。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@dpuzeg.cn

文章转载自:自互联网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP