Home All Groups Group Topic Archive Search About

Long Lease Times a problem?

Author
19 Sep 2006 4:03 AM
Jayme Pechan
I'm very confused about whats wrong with doing long Renewal times for the
ISponsor interface.  Basically, I had been returning
TimeSpan.FromSeconds(20) from my Renewal function and all was well.  I then
thought it might be a good idea to increase the lease time so I use less
overhead.   As a result, I changed the Renewal function to return
TimeSpan.FromMinutes(2) instead and I thought everything was good.  For some
reason, when I do this, my object disappears after 6 minutes before it calls
my Renewal function for the 3rd time.  It drives me crazy.  I looked all
over and can't find a max value that should be specified from this method
but it sure doesn't seem to work right when I put too long of a time in
there.  Here's the function that breaks it.

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags =
SecurityPermissionFlag.Infrastructure)]
public TimeSpan Renewal(ILease lease)
{
    Debug.WriteLine("***** Renewal called");
    return TimeSpan.FromMinutes(2);
}

All the docs use seconds for the examples so I guess I should stick with
that but why wouldn't this work?  Anyone know?  I did a timer loop to check
my lease time left and it counts down from 2 minutes just fine but like I
said, eventually these ILease functions throw exceptions because the object
disappears on me.  Oh and I had my SponsorTimeout as 1 minute and it freed
my object on the third iteration after 9 seconds and before it called my
Renewal function.

I'm using .NET 2.0.  Is this a bug or am I missing something?  Any thoughts?

Thanks, Jayme.

Author
19 Sep 2006 4:36 PM
Jayme Pechan
Clearly there is something I don't understand about object sponsorship.
Could someone tell me what is wrong with the following code.  I wanted to
use this class to maintain objects as long as I wanted them to stay around.
The problem is that the class doesn't work.  It stops working after a period
of time (after 5 minutes or so I think).  The reason I know there is
something wrong with this code is that the ClientSponser class seems to work
fine used in the same way.  I figure the ClientSponsor class must do
something else.

Thanks for any help in advance.

Jayme

class LifetimeManagment : MarshalByRefObject , ISponsor
{
    public bool MaintainObjectLease(object objToLease)
    {
        MarshalByRefObject obj = objToLease as MarshalByRefObject;
        if (obj != null)
        {
            if (!RemotingServices.IsObjectOutOfAppDomain(obj))
                return false;
            ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
            try
            {
                lease.Register(this); // register as a Sponsor for this
object
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
        return false;
    }
    public bool RemoveObjectLease(object objLeased)
    {
        MarshalByRefObject obj = objLeased as MarshalByRefObject;
        if (obj != null)
        {
            if (!RemotingServices.IsObjectOutOfAppDomain(obj))
                return false;
            ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
            try
            {
                lease.Unregister(this);
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
        return false;
    }
    [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags =
SecurityPermissionFlag.Infrastructure)]
    public TimeSpan Renewal(ILease lease)
    {
        return TimeSpan.FromMinutes(2);
    }
}

AddThis Social Bookmark Button