2

我有一个应用程序,它在一个进程中创建多个 AppDomains,并通过远程处理在它们之间进行通信。我为所有对象创建赞助商,以防止它们被 GCed。

但是,无论如何,有些人最终还是被 GCed 了。经过一番调查,我确定根据InitialLeaseTime我的远程对象上的设置,我的赞助商要么永远不会被调用,要么会被调用几次,然后再也不被调用。

我的赞助商(为简洁起见,我删除了一些健全性检查):

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;
        }
    }
}

我的测试用例

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;
    }
}

如果我离开InitialLeaseTime5 分钟,.NET 默认值,我的赞助商将永远不会被调用。如果我将其设置为 2 分钟,它将被调用两次,然后再也不会调用。如果我将它设置为 1 分钟,它将被连续调用并按照我期望的默认值工作。

更新

从那以后,我确定ILease我的赞助商自己的对象正在被 GCed。他们从默认的 5 分钟租赁时间开始,这解释了我的赞助商被呼叫的频率。当我将 my 设置InitialLeaseTime为 1 分钟时,ILease对象会不断更新,因为它们RenewOnCallTime的默认值为 2 分钟。

我究竟做错了什么?我看不到为我的赞助商的租赁对象创建赞助商的方法。

4

2 回答 2

4

自从提出这个问题以来已经有很长时间了,但是我今天遇到了这个问题,几个小时后,我想通了。5 分钟的问题是因为必须从 MarshalByRefObject 继承的 Sponsor 也具有关联的租约。它是在您的客户端域中创建的,并且您的主机域具有对您的客户端域中的引用的代理。这将在默认的 5 分钟后过期,除非您在 Sponsor 类中覆盖 InitializeLifetimeService() 方法,或者此赞助商有自己的赞助商使其不会过期。

有趣的是,我通过在赞助商的 InitializeLifetimeService() 覆盖中返回 Null 来克服这个问题,给它一个无限的时间跨度租约,我创建了我的 ISponsor 实现以在 Host MBRO 中删除它。

于 2017-12-21T04:13:11.547 回答
1

我发布了一个非常相似的问题并回答“赞助商的续订功能停止被调用”。甚至可能是同样的问题。

我通过将赞助商放在服务器而不是客户端上来解决它。服务器端赞助商似乎被可靠地调用到足以保持远程对象的活动。我知道这在很多情况下都不是一个非常安全的解决方案,因为客户必须主动断开赞助商,而不是让租约到期。

于 2013-09-23T01:38:17.100 回答