本文整理汇总了C#中System.MarshalByRefObject.InitializeLifetimeService方法的典型用法代码示例。如果您正苦于以下问题:C# MarshalByRefObject.InitializeLifetimeService方法的具体用法?C# MarshalByRefObject.InitializeLifetimeService怎么用?C# MarshalByRefObject.InitializeLifetimeService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.MarshalByRefObject
的用法示例。
在下文中一共展示了MarshalByRefObject.InitializeLifetimeService方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetObjectSink
// Initiates the creation of a lease
// Creates a sink for invoking a renew on call when an object is created.
public IMessageSink GetObjectSink(MarshalByRefObject obj,
IMessageSink nextSink)
{
bool fServer;
ServerIdentity identity = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);
BCLDebug.Assert(identity != null, "[LifetimeServices.GetObjectSink] identity != null");
// NOTE: Single Call objects do not have a lease associated with it because they last
// only for the duration of the call.
// Singleton objects on the other hand do have leases associated with them and they can
// be garbage collected.
if (identity.IsSingleCall())
{
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease SingleCall",obj,", NextSink "+nextSink);
return nextSink;
}
// Create lease. InitializeLifetimeService is a virtual method which can be overridded so that a lease with
// object specific properties can be created.
Object leaseObj = obj.InitializeLifetimeService();
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, return from InitializeLifetimeService obj ",obj,", lease ",leaseObj);
// InitializeLifetimeService can return a lease in one of conditions:
// 1) the lease has a null state which specifies that no lease is to be created.
// 2) the lease has an initial state which specifies that InitializeLifeTimeService has created a new lease.
// 3) the lease has another state which indicates that the lease has already been created and registered.
if (leaseObj == null)
{
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease ",obj,", NextSink "+nextSink);
return nextSink;
}
if (!(leaseObj is System.Runtime.Remoting.Lifetime.ILease))
throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Lifetime_ILeaseReturn"), leaseObj));
ILease ilease = (ILease)leaseObj;
if (ilease.InitialLeaseTime.CompareTo(TimeSpan.Zero) <= 0)
{
// No lease
{
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease because InitialLeaseTime is Zero ",obj);
if (ilease is System.Runtime.Remoting.Lifetime.Lease)
{
((Lease)ilease).Remove();
}
return nextSink;
}
}
Lease lease = null;
lock(identity)
{
if (identity.Lease != null)
{
// Lease already exists for object, object is being marsalled again
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, Lease already exists for object ",obj);
lease = (Lease)identity.Lease;
lease.Renew(lease.InitialLeaseTime); // Reset initial lease time
}
else
{
// New lease
if (!(ilease is System.Runtime.Remoting.Lifetime.Lease))
{
// InitializeLifetimeService created its own ILease object
// Need to create a System.Runtime.Remoting.Lease object
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, New Lease, lease not of type Lease ",obj);
lease = (Lease)LifetimeServices.GetLeaseInitial(obj);
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = ilease.InitialLeaseTime;
lease.RenewOnCallTime = ilease.RenewOnCallTime;
lease.SponsorshipTimeout = ilease.SponsorshipTimeout;
}
}
else
{
// An object of Type Lease was created
BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, New Lease, lease is type Lease ",obj);
lease = (Lease)ilease;
}
// Put lease in active state
// Creation phase of lease is over, properties can no longer be set on lease.
identity.Lease = lease; // Place lease into identity for object
// If the object has been marshaled activate
//.........这里部分代码省略.........
示例2: GetObjectSink
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
bool flag;
ServerIdentity identity = (ServerIdentity) MarshalByRefObject.GetIdentity(obj, out flag);
if (!identity.IsSingleCall())
{
object obj2 = obj.InitializeLifetimeService();
if (obj2 == null)
{
return nextSink;
}
if (!(obj2 is ILease))
{
throw new RemotingException(Environment.GetResourceString("Remoting_Lifetime_ILeaseReturn", new object[] { obj2 }));
}
ILease lease = (ILease) obj2;
if (lease.InitialLeaseTime.CompareTo(TimeSpan.Zero) <= 0)
{
if (lease is Lease)
{
((Lease) lease).Remove();
}
return nextSink;
}
Lease leaseInitial = null;
lock (identity)
{
if (identity.Lease != null)
{
leaseInitial = identity.Lease;
leaseInitial.Renew(leaseInitial.InitialLeaseTime);
}
else
{
if (lease is Lease)
{
leaseInitial = (Lease) lease;
}
else
{
leaseInitial = (Lease) LifetimeServices.GetLeaseInitial(obj);
if (leaseInitial.CurrentState == LeaseState.Initial)
{
leaseInitial.InitialLeaseTime = lease.InitialLeaseTime;
leaseInitial.RenewOnCallTime = lease.RenewOnCallTime;
leaseInitial.SponsorshipTimeout = lease.SponsorshipTimeout;
}
}
identity.Lease = leaseInitial;
if (identity.ObjectRef != null)
{
leaseInitial.ActivateLease();
}
}
}
if (leaseInitial.RenewOnCallTime > TimeSpan.Zero)
{
return new LeaseSink(leaseInitial, nextSink);
}
}
return nextSink;
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:62,代码来源:LeaseLifeTimeServiceProperty.cs