저는 하나의 프로세스에서 여러 AppDomains를 생성하고 원격을 통해 이들간에 통신하는 앱을 가지고 있습니다. 나는 모든 객체에 대해 스폰서를 만들어 GCed가되지 않도록합니다.원격 스폰서가 호출되지 않습니다.
그러나 일부는 GCed로 처리됩니다. 조사가 끝난 후 원격 객체에 설정된 InitialLeaseTime
에 따라 스폰서가 호출되지 않았거나 몇 번 호출 된 다음 다시는 호출되지 않습니다. 나는 InitialLeaseTime
을 5 분의두면
class Program : MarshalByRefObject
{
static void Main(string[] args)
{
AppDomain ad = AppDomain.CreateDomain("Remote");
Program obj = (Program)ad.CreateInstanceAndUnwrap(
typeof(Program).Assembly.FullName,
typeof(Program).FullName);
using (new Sponsor(obj))
{
// sleep for 6 minutes.
// 5 seems to be the point where it gets GCed.
Thread.Sleep(6 * 60 * 1000);
// throws a RemotingException
obj.Ping();
}
}
void Ping()
{
}
public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
// this is the .NET default. if used, the lease is never renewed.
//lease.InitialLeaseTime = TimeSpan.FromMinutes(5);
// if uncommented, lease is renewed twice and never again.
//lease.InitialLeaseTime = TimeSpan.FromMinutes(2);
// if uncommented, lease is renewed continually.
//lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
}
return lease;
}
}
:
class Sponsor : MarshalByRefObject, ISponsor, IDisposable
{
ILease lease;
public Sponsor(MarshalByRefObject mbro)
{
lease = (ILease)RemotingServices.GetLifetimeService(mbro);
lease.Register(this);
}
public TimeSpan Renewal(ILease lease)
{
return this.lease != null ? lease.InitialLeaseTime : TimeSpan.Zero;
}
public void Dispose()
{
if(lease != null)
{
lease.Unregister(this);
lease = null;
}
}
}
내 테스트 케이스 :
내 스폰서는 (나는 간결 확인 약간의 정신을 삭제했습니다) .NET 기본값. 스폰서는 절대 호출되지 않습니다. 2 분으로 설정하면 두 번 호출되고 다시는 호출되지 않습니다. 1 분으로 설정하면, 지속적으로 호출되고 기본값이 어떻게 작동하는지 예상합니다.
업데이트
내가 결정 이후로했습니다 내 스폰서의 ILease
객체 자체가 GCed되는 것을. 기본 5 분 임대 시간부터 시작하여 스폰서가 얼마나 자주 전화를 받는지 설명합니다. InitialLeaseTime
을 1 분으로 설정하면 ILease
개체는 RenewOnCallTime
이 기본값 인 2 분이므로 지속적으로 갱신됩니다.
내가 뭘 잘못하고 있니? 스폰서의리스 오브젝트에 대한 스폰서를 만드는 방법이 없습니다.
귀하의 테스트 값은 사용 된 스폰서가 클라이언트 객체이고 또한 (기본값으로) 만료되었으므로 스폰서 목록에서 삭제되었으며 임대를 갱신하지 않았 음을 나타냅니다. –
그 이유는'System.Runtime.Remoting.Lifetime.ClientSponsor. InitializeLifetimeService'가 null을 반환합니다. –