Remote Object Lifetime using VB.NET
In this article I will explain you about Remote Object Lifetime in VB.NET.
Unexpected loss of remote connections has been a fact of life since the advent of network communications. Countless lines of code have been written to make endpoints aware of a dropped connection and to recover-either with a reconnection or a graceful shutdown.
DCOM uses a "ping" mechanism. The client pings the server at regular intervals. If the server doesn't receive notification from the client in a specified time, it determines the client has disconnected, for whatever reason, and is free to clean up resources held for the client. How does the VB.NET Remoting Framework handle a dropped connection?
In the client's application domain, the answer is easy. Once the remote connection disappears, a call on a method or property causes an exception to be thrown. Catch the exception and handle it in some prescribed manner. The exception may be a System.Net.WebException or a System.Runtime.Remoting.RemotingException.
What happens with client-activated remote objects on the server? How does the server detect that the client is no longer alive? The VB.NET Remoting solution is a form of lease management called Leasing Distributed Garbage Collector (LDGC). Leases implement the System.Runtime.Remoting.Lifetime.ILease interface, which contains three configurable properties:
InitialLeaseTime is the initial lifetime of the remote object. The default setting is five minutes.
RenewOnCallTime is the amount of time to renew the lease for, after each method call. The default setting is two minutes.
SponsorshipTimeout is the amount of time the remoting framework waits for a sponsor to become available. The default setting is two minutes.
The default lease values can be changed while the ILease.CurrentState is set to System.Runtime.Remoting.Lifetime.LeaseState.Initial. Once the object has been marshaled, the lease's current state is set to active. Any attempt to alter the properties, once the lease is active, will cause an exception to be thrown. When the lease expires, the CurrentLeaseState property is internally set to expired and the remote object will be "garbage collected" at some point.
Leases can be renewed in three ways:
It is automatic when the client calls a method. The lease time is set to the current lease time remaining plus the RenewOnCallTime.
The client can renew the lease by using the remote object or transparent proxy as a parameter in calling RemotingServices.GetLifetimeService(). This call returns an ILease, which can be used to call Renew ().
A sponsor is another alternative to lease renewal. The client can use the supplied System.Runtime.Remoting.Lifetime.ClientSponsor or create a sponsor that implements the System.Runtime.Remoting.Lifetime.ISponsor interface. The client registers the sponsor with ILease.Register(). When the remote object's lease expires, the leasing infrastructure calls the sponsor's Renewal(). The sponsor can then specify a new System.Runtime.Remoting.Lifetime.TimeSpan.
The server can change the default leasing values on all remote objects by setting the properties on System.Runtime.Remoting.Lifetime.LifetimeServices. Individual remote objects can also set ILease properties by overriding MarshalByRefObject.InitializeLiftetimeService() as shown in Listing 25.6.
Listing 25.6: SimpleObject.cs
Public Overrides Function InitializeLifetimeService() As Object
Dim lease As ILease = Nothing
lease = DirectCast(MyBase.InitializeLifetimeService(), ILease)
If lease IsNot Nothing Then
lease.InitialLeaseTime = TimeSpan.FromSeconds(5)
lease.RenewOnCallTime = TimeSpan.FromSeconds(5)
lease.SponsorshipTimeout = TimeSpan.FromSeconds(5)
Leasing and lease management do not apply to server-activated SingleCall-mode remote objects. Remember that they are created and then offered up to the GC on every method call. Microsoft's
Introduction to VB.NET Remoting, updated July 2001 in the MSDN library, states that server-activated "Singleton objects are also subject to lifetime management." This seems reasonable, and in fact, InitializeLifetimeService() is called in Singleton mode but not in SingleCall mode.
Hope this article would have helped you in understanding Remote Object Lifetime in VB.NET.