C# Source Code: Changing the lifetime of a client activated object (CAO) in Remoting
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Changing the lifetime of a client activated object (CAO) in Remoting
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, April 13, 2004
Hits:
759
Category:
Remoting/Web Services
Article:
The .NET framework manages remotly created objects using a lease-based object lifetime. This means that each server-side object is associated with a lease upon it's creation. When the runtime activates an instance of either a well-known Singleton or a client-activated remote object, it asks the object for a lease by calling the object's InitializeLifetimeService method (inherited from MarshalByRefObject). This returns a lease object with the ILease interface. Below are the four main properties which are used to manage the objects lifetime. 1) InitialLeaseTime. This is a TimeSpan value that determines how long a lease is valid for. When the object is first created the CurrentLeaseTime will equal the InitialLeaseTime. A value of zero indicates the lease will never expire. 2) CurrentLeaseTime. This is amount of time remaining until the lease expires (read only property). 3) RenewOnCallTime. This is the amount of time to add to the CurrentLeaseTime each time a method is called on the object. Note, the RenewOnCallTime amount is only added to the CurrentLeaseTime if the CurrentLeaseTime is less than the RenewOnCallTime. 4) SponsorshipTimeout. The amount of time to wait for a sponsor of a lease to responsd. When the CurrentLeaseTime time reaches zero, the framework looks for any registered sponsors. If not sponsors are found then the object will be marked for garbage collection. If a client referencing this object then attempts to use the object it will receive an exception. To change the default lease times you can override the InitializeLifetimeService method. The code below demonstrates the key points you will need to consider when changing this: //(see http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=15&PostID=1&NumReplies=0 for the rest of the code) namespace vbUsers.Remoting.Server { //Class containing the implimentation of the remote data object class RemoteData: MarshalByRefObject, IRemoteData { int myvalue; //Override the MarshalByRefObject.InitializeLifetimeService call //to reduce the lifespan of the object to 1 second public override object InitializeLifetimeService() { Console.WriteLine("RemoteData.InitializeLifetimeService"); ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { //Set the initial time for the lease (default is 5 mins) lease.InitialLeaseTime = TimeSpan.FromSeconds(1); //Set the amount of time to wait for the sponsor to return with a lease renewal time lease.SponsorshipTimeout = TimeSpan.FromSeconds(1); //Set the amount of time increase the CurrentLeaseTime by for each call //to the remote object (defalt is 2 mins) lease.RenewOnCallTime = TimeSpan.FromSeconds(1); } //Return the result return lease; /* To create a lease that never expires use: return null; */ } } class ServerStartup { static void Main(string[] args) { //Change the poll time to 1 second (default is 10 secs) LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1); HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); //Add rest of code here... } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet