12.11 Control the Lifetime of a Remote Object


Problem

You want to configure how long a singleton or client-activated object lives while not in use.

Solution

Configure a lease policy by using configuration file settings, override the MarshalByRefObject.InitializeLifetimeService method, or implement a custom lease provider.

Discussion

If a remotable object uses single-call activation, it will be automatically destroyed at the end of each method call. This behavior changes with client- activated and singleton objects, which are given a longer lifetime dictated by a lifetime lease . With the default settings, a remote object will be automatically destroyed if it's inactive for two minutes, provided it has been in existence for at least five minutes.

The component host, remote object, and client each have the opportunity to change lifetime settings.

  • The component host can specify different lease lifetime defaults in the configuration file. These settings will apply to all the remotable objects it hosts .

  • The remote class can override its GetLifetimeService method to modify its initial lease settings using the provided ILease object.

  • The client can call the MarshalByRefObject.GetLifetimeService method with a specific remote object to retrieve an ILease instance. The client can then call the ILease.Renew method to specify a minimum amount of time the object should be kept alive .

Here's an example that uses the first approach: a component host configuration file using the < lifetime > tag. These lease settings apply to all the remote objects created by the component host. Use a trailing M for minutes or an S to indicate seconds. It gives remote objects an initial lifetime of 10 minutes. When a client accesses the object, its lifetime is automatically renewed to at least three minutes.

 <configuration>   <system.runtime.remoting>     <application>              <service>          <wellknown              mode = "Singleton"              type="RemoteObjects.RemoteObject, RemoteObjects"              objectUri="RemoteObject" />       </service>              <channels>         <channel ref="tcp" port="9080" />       </channels>   <  lifetime leaseTime = "10M"   renewOnCallTime = "3M" /  >              </application>   </system.runtime.remoting> </configuration> 

Another common approach is to override the InitializeLifetimeService method so a remote object takes control of its own lifetime. The following code shows the code you could add to a remote class to give it a default 10-minute lifetime and 5-minute renewal time.

 public override object InitializeLifetimeService() {     ILease lease = MyBase.InitializeLifetimeService();     // Lease can only be configured if it is in an initial state.     if (lease.CurrentState == LeaseState.Initial) {         lease.InitialLeaseTime = TimeSpan.FromMinutes(10);         lease.RenewOnCallTime = TimeSpan.FromMinutes(5);     }     return lease; } 

If you wanted the object to have an unlimited lifetime, simply return a null reference instead of an ILease object. This is most commonly the case if you are creating a singleton object that needs to run independently (and permanently), even if clients aren't currently using it.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net