Let us assume that Server S starts listening for the
client's call. The very first client, CLIENT_A, creates a remote object
in OnStart(). At this moment no activity happens at the server, meaning the
remote object instance creation does not request any from server. CLIENT_A
makes the first remote method call, then server initializes its class object
(so runs default constructor New()) at the first time. Hence, the server
initializes all member variables ONLY at the very first remote method call from
the very first client. Remember, this happens only once unless and until the server
remote application is stopped and started again. This is true even after some
time if no client calls come for long (there is a default idle timeout set
for the server which I will discuss later), and any new/old client starts
method call again.
That is why we can do state management among several clients
in SAO-Singleton activation, since member variable remains common and active in
the server memory during its lifetime. All these are possible because the server
has only one remote object instance in the member at any case.
SAO can support only default constructor. SAO can never be
more than one instance of the server object at any point of time.
But Singleton server object would be garbage collected at the
server if it remains inactive up to its default LEASE-BASE LIFETIME. Default
time is 5 minutes for the first time and 2 minutes for the rest. Lease time is
the duration unto which object should remain in memory, preventing it from
automatic garbage collection.
Once the server object instance is created it will exist and
server all clients unless and until the server application gets stopped
intentionally or unintentionally.
To increase or decrease this default lifetime, add the following
namespace in server class:
You can override the InitializeLifetimeService method as
given below (this sample code is taken from msdn).
Listing 4: Overriding InitializeLifetimeService
Public Overrides Function InitializeLifetimeService() As Object
Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)
If lease.CurrentState = LeaseState.Initial Then
lease.InitialLeaseTime = TimeSpan.FromMinutes(1)
lease.SponsorshipTimeout = TimeSpan.FromMinutes(2)
lease.RenewOnCallTime = TimeSpan.FromSeconds(2)
You can explore more on it if you wish. See MSDN for more information about it.
For infinite lease time set ease.InitialLeaseTime