Remoting赞助商停止被召唤

问题描述 投票:2回答:2

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

但是,有些人最终还是被GC了。经过一些调查后,我已经确定,根据我的远程对象上设置的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;
    }
}

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

更新

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

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

.net remoting appdomain
2个回答
4
投票

问这个问题已经有很长一段时间了,但我今天碰到了这个问题,经过几个小时后,我发现了这个问题。 5分钟的问题是因为您必须从MarshalByRefObject继承的赞助商也有相关的租约。它是在您的客户端域中创建的,您的主机域具有客户域中引用的代理。这将在默认的5分钟后到期,除非您覆盖赞助商类中的InitializeLifetimeService()方法,或者此赞助商拥有自己的赞助商,以防止其过期。

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


1
投票

我发布了一个非常类似的问题并回答了"Sponsor's Renewal function stops being called"。它甚至可能是同样的问题。

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

© www.soinside.com 2019 - 2024. All rights reserved.