Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c# - How do the ISponsor and ILease interfaces work?

I've created a object that inherits from MarshalByRefObject and ISponsor. In my implementation of ISponsor I just return a timespan to indicate how long I want the object renewed for.

When I call InitializeLifetimeService() to get an ILease reference to be passed into my ISponsor object it never appears to be used from examples I've seen.

ISponsor just seems to return a TimeSpan without actually using the ILease reference. But I'm sure there is more going on here since remoting is involved.

How do ISponsor and ILease work, specifically in terms of object lifetime renewal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In a client app where you want to extend the lease of a remote object you would typically obtain an ILease interface for the object by calling

ILease lease = (ILease)RemotingServices.GetLifetimeService( remoteObject );

and then pass it your custom sponsor object

lease.Register( customSponsor );

where your custom sponsor class will look something like this:

private class CustomSponsor : MarshalByRefObject, ISponsor
{
    public TimeSpan Renewal(ILease lease)
    {
        Debug.Assert(lease.CurrentState == LeaseState.Active);
        //Renew lease by 5 minutes

        return TimeSpan.FromMinutes(5);
    }
}

For more information check out this helpful MSDN article on leasing and sponsorship. http://msdn.microsoft.com/en-us/magazine/cc300474.aspx

The link no longer works - it was in the December 2003 issue though which can be downloaded in CHM format from the same page.

A wayback machine link is here:

https://web.archive.org/web/20080906214332/http://msdn.microsoft.com/en-us/magazine/cc300474.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...