服务器配置
服务器端激活对象(wellknown):
<service>
<wellknown mode="Singleton" type="General.HelloServer, General" objectUri="SayHello" />
</service>
(singleton为激活方式,服务器只有一个对象实例;General.HelloServer 为远程对象的类型;General为远程对象的 程序集)
客户端激活对象(activated):
<service>
<activated type="General.HelloServer,General">
</service>
( 服务器为没有个客户端新建一个对象,与singlecall类似,使用cao可以保存客户端的状态)
<channels><channel ref="http" port="8086"></channels>
客户端配置
服务器端激活对象(wellknown):
<client>
<wellknown type="General.HelloServer,General"url="http://localhost:8087/SayHello" />
</client>
客户端激活对象(activated):
<client url=http://localhost:8086>
<activated type="General.HelloServer,General"
</client>
<channels><channel ref="http" port="0"></channels>
加载配置文件
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
RemotingConfiguration.Configure(@"Server.exe.config");
(两种方式都可以,一般情况下用第一种方式比较好)
使用.net remoting 的用处:
1,进程间的通信方便
2,安全性能比较高
3,可以使用直接操作或者传递对象
租约管理
租用是个封装了TimeSpan类型值的对象,用以管理远程对象的生存期。在.Net Remoting中提供了定义租用功能的ILease接口。当Remoting通过SingleTon模式或客户端激活模式来激活远程对象时,租用对象调用从System.MarshalByRefObject类继承的InitializeLifetimeService方法,向对象请求租用。
ILease接口定义了有关生命周期的属性,均为TimeSpan值,具体介绍如下:
l InitialLeaseTime:初始化有效时间,默认值为300秒,如果为0,表示永不过期;
l RenewOnCallTime:调用远程对象一个方法时的租用更新时间,默认值为120秒;
l SponsorshipTimeout:超时值,通知Sponsor(发起人)租用过期后,Remoting会等待的时间,默认值为120秒;
l CurrentLeaseTime:当前租用时间,首次获得租用时,为InitializeLeaseTime的值。
Remoting的远程对象因为继承了MarshalByRefObject类,因此默认继承了InitializeLifetimeService方法,那么租用的相关属性为默认值。如果要改变这些设置,可以在远程对象中重写该方法。例如:
public override object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.RenewOnCallTime = TimeSpan.FromSeconds(20); } return lease; } |
public override object InitializeLifetimeService() { return null; } |