mscorlib(4.0.0.0) API with additions
LifetimeServices.cs
2 using System.Security;
4 using System.Threading;
5 
7 {
9  [SecurityCritical]
10  [ComVisible(true)]
11  public sealed class LifetimeServices
12  {
13  private static bool s_isLeaseTime = false;
14 
15  private static bool s_isRenewOnCallTime = false;
16 
17  private static bool s_isSponsorshipTimeout = false;
18 
19  private static long s_leaseTimeTicks = TimeSpan.FromMinutes(5.0).Ticks;
20 
21  private static long s_renewOnCallTimeTicks = TimeSpan.FromMinutes(2.0).Ticks;
22 
23  private static long s_sponsorshipTimeoutTicks = TimeSpan.FromMinutes(2.0).Ticks;
24 
25  private static long s_pollTimeTicks = TimeSpan.FromMilliseconds(10000.0).Ticks;
26 
27  private static object s_LifetimeSyncObject = null;
28 
29  private static object LifetimeSyncObject
30  {
31  get
32  {
33  if (s_LifetimeSyncObject == null)
34  {
35  object value = new object();
36  Interlocked.CompareExchange(ref s_LifetimeSyncObject, value, null);
37  }
38  return s_LifetimeSyncObject;
39  }
40  }
41 
45  public static TimeSpan LeaseTime
46  {
47  get
48  {
49  return GetTimeSpan(ref s_leaseTimeTicks);
50  }
51  [SecurityCritical]
52  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
53  set
54  {
55  lock (LifetimeSyncObject)
56  {
57  if (s_isLeaseTime)
58  {
59  throw new RemotingException(Environment.GetResourceString("Remoting_Lifetime_SetOnce", "LeaseTime"));
60  }
61  SetTimeSpan(ref s_leaseTimeTicks, value);
62  s_isLeaseTime = true;
63  }
64  }
65  }
66 
70  public static TimeSpan RenewOnCallTime
71  {
72  get
73  {
74  return GetTimeSpan(ref s_renewOnCallTimeTicks);
75  }
76  [SecurityCritical]
77  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
78  set
79  {
80  lock (LifetimeSyncObject)
81  {
82  if (s_isRenewOnCallTime)
83  {
84  throw new RemotingException(Environment.GetResourceString("Remoting_Lifetime_SetOnce", "RenewOnCallTime"));
85  }
86  SetTimeSpan(ref s_renewOnCallTimeTicks, value);
87  s_isRenewOnCallTime = true;
88  }
89  }
90  }
91 
95  public static TimeSpan SponsorshipTimeout
96  {
97  get
98  {
99  return GetTimeSpan(ref s_sponsorshipTimeoutTicks);
100  }
101  [SecurityCritical]
102  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
103  set
104  {
105  lock (LifetimeSyncObject)
106  {
107  if (s_isSponsorshipTimeout)
108  {
109  throw new RemotingException(Environment.GetResourceString("Remoting_Lifetime_SetOnce", "SponsorshipTimeout"));
110  }
111  SetTimeSpan(ref s_sponsorshipTimeoutTicks, value);
112  s_isSponsorshipTimeout = true;
113  }
114  }
115  }
116 
120  public static TimeSpan LeaseManagerPollTime
121  {
122  get
123  {
124  return GetTimeSpan(ref s_pollTimeTicks);
125  }
126  [SecurityCritical]
127  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
128  set
129  {
130  lock (LifetimeSyncObject)
131  {
132  SetTimeSpan(ref s_pollTimeTicks, value);
133  if (LeaseManager.IsInitialized())
134  {
135  LeaseManager.GetLeaseManager().ChangePollTime(value);
136  }
137  }
138  }
139  }
140 
141  private static TimeSpan GetTimeSpan(ref long ticks)
142  {
143  return TimeSpan.FromTicks(Volatile.Read(ref ticks));
144  }
145 
146  private static void SetTimeSpan(ref long ticks, TimeSpan value)
147  {
148  Volatile.Write(ref ticks, value.Ticks);
149  }
150 
152  [Obsolete("Do not create instances of the LifetimeServices class. Call the static methods directly on this type instead", true)]
154  {
155  }
156 
157  [SecurityCritical]
158  internal static ILease GetLeaseInitial(MarshalByRefObject obj)
159  {
160  ILease lease = null;
161  LeaseManager leaseManager = LeaseManager.GetLeaseManager(LeaseManagerPollTime);
162  lease = leaseManager.GetLease(obj);
163  if (lease == null)
164  {
165  lease = CreateLease(obj);
166  }
167  return lease;
168  }
169 
170  [SecurityCritical]
171  internal static ILease GetLease(MarshalByRefObject obj)
172  {
173  ILease lease = null;
174  LeaseManager leaseManager = LeaseManager.GetLeaseManager(LeaseManagerPollTime);
175  return leaseManager.GetLease(obj);
176  }
177 
178  [SecurityCritical]
179  internal static ILease CreateLease(MarshalByRefObject obj)
180  {
181  return CreateLease(LeaseTime, RenewOnCallTime, SponsorshipTimeout, obj);
182  }
183 
184  [SecurityCritical]
185  internal static ILease CreateLease(TimeSpan leaseTime, TimeSpan renewOnCallTime, TimeSpan sponsorshipTimeout, MarshalByRefObject obj)
186  {
187  LeaseManager.GetLeaseManager(LeaseManagerPollTime);
188  return new Lease(leaseTime, renewOnCallTime, sponsorshipTimeout, obj);
189  }
190  }
191 }
Controls the.NET remoting lifetime services.
Describes a set of security permissions applied to code. This class cannot be inherited.
Defines a lifetime lease object that is used by the remoting lifetime service.
Definition: ILease.cs:8
static void Write(ref bool location, bool value)
Writes the specified value to the specified field. On systems that require it, inserts a memory barri...
Definition: Volatile.cs:186
Definition: __Canon.cs:3
static TimeSpan LeaseTime
Gets or sets the initial lease time span for an T:System.AppDomain.
static TimeSpan FromMilliseconds(double value)
Returns a T:System.TimeSpan that represents a specified number of milliseconds.
Definition: TimeSpan.cs:477
static TimeSpan FromMinutes(double value)
Returns a T:System.TimeSpan that represents a specified number of minutes, where the specification is...
Definition: TimeSpan.cs:492
long Ticks
Gets the number of ticks that represent the value of the current T:System.TimeSpan structure.
Definition: TimeSpan.cs:84
static TimeSpan RenewOnCallTime
Gets or sets the amount of time by which the lease is extended every time a call comes in on the serv...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static TimeSpan FromTicks(long value)
Returns a T:System.TimeSpan that represents a specified time, where the specification is in units of ...
Definition: TimeSpan.cs:545
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Contains methods for performing volatile memory operations.
Definition: Volatile.cs:8
static TimeSpan LeaseManagerPollTime
Gets or sets the time interval between each activation of the lease manager to clean up expired lease...
static bool Read(ref bool location)
Reads the value of the specified field. On systems that require it, inserts a memory barrier that pre...
Definition: Volatile.cs:15
LifetimeServices()
Creates an instance of T:System.Runtime.Remoting.Lifetime.LifetimeServices.
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
SecurityPermissionFlag
Specifies access flags for the security permission object.
static TimeSpan SponsorshipTimeout
Gets or sets the amount of time the lease manager waits for a sponsor to return with a lease renewal ...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
The exception that is thrown when something has gone wrong during remoting.
Enables access to objects across application domain boundaries in applications that support remoting.