.NET 远程处理对象生命周期

发布于 2024-11-05 09:56:55 字数 670 浏览 3 评论 0原文

我写了这段代码:

public class Message : MarshalByRefObject, IMessage
{
    ...

    public override object InitializeLifetimeService()
    {
        ILease leas = (ILease) base.InitializeLifetimeService();
        if (leas != null)
        {
            if(leas.CurrentState == LeaseState.Initial)
            {
                leas.InitialLeaseTime = TimeSpan.FromMilliseconds(2000);
                leas.SponsorshipTimeout = TimeSpan.Zero;
                leas.RenewOnCallTime = TimeSpan.Zero;
            }
        }
        return leas;
    }
}

InitializeLifetimeService 的覆盖是否会在 2 秒后对象不再被“垃圾收集”?我的意思是,无论该实例是否被远程访问,都是独立的。

谢谢。

I wrote this code:

public class Message : MarshalByRefObject, IMessage
{
    ...

    public override object InitializeLifetimeService()
    {
        ILease leas = (ILease) base.InitializeLifetimeService();
        if (leas != null)
        {
            if(leas.CurrentState == LeaseState.Initial)
            {
                leas.InitialLeaseTime = TimeSpan.FromMilliseconds(2000);
                leas.SponsorshipTimeout = TimeSpan.Zero;
                leas.RenewOnCallTime = TimeSpan.Zero;
            }
        }
        return leas;
    }
}

Does the override of InitializeLifetimeService grantee that after 2 seconds the object is no "garbage collected"? I mean, independently if this instance was remotely accessed or not.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-11-12 09:56:55

为了确保对象租约结束,您只需将生命周期服务的轮询间隔设置为低于您设置的生命周期 2 秒。

您可以在服务器对象的构造函数中执行此操作:

// just a sample value of 1 second
LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1); 

现在即使您在对象上调用方法,
它仍然会被收集,因为您设置了:

leas.RenewOnCallTime = TimeSpan.Zero

To make sure the object lease ends you only need to set the life time service's poll interval to something lower than the 2 seconds you set as a life time.

you can do this in the server object`s constructor:

// just a sample value of 1 second
LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1); 

now even if you call mehods on the object ,
it will still be collected, because you set the:

leas.RenewOnCallTime = TimeSpan.Zero
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文