指定 WCF 服务会话无限期持续

发布于 2024-09-11 08:07:51 字数 993 浏览 8 评论 0原文

我是 WCF 的新手,所以也许最好以另一种方式完成这件事。

现在我有一组 WCF 服务,但我正在尝试构建每周发送电子邮件的功能。为此,我使用下面的代码构建了另一个 WCF 服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AutomaticSessionShutdown = false)]
public class TimerService : ITimerService
{
    private static Timer timer;
    private static TimeSpan tSpan = new TimeSpan(0, 0, 20, 0);
    private static OtherService Ref = new OtherService();

    public void ToggleEmailTimer(bool enabled)
    {
        if (enabled)
            timer = new Timer(new TimerCallback(TimerElapsed), null, tSpan, tSpan);
        else
        {   
            if(timer != null)
                timer.Dispose();
        }
    }

    private void TimerElapsed(object state)
    {
        Ref.SendWeekly();
    }
}

它开始被禁用,然后我从 aspx 页面启用它。为了测试,我已经设法使这项工作间隔 10 分钟,并且它似乎在设置周围的某个地方中断间隔为15分钟。

对我来说,WCF 服务会话似乎因不活动而到期,这可以解释为什么计时器会停止。有没有办法指定 WCF 服务的生命周期,以便我可以从 aspx 页面启用计时器,然后退出,并且计时器服务将持续存在?我已经看到有关设置超时值的信息,但我仍然不清楚这是否适用。

I'm new to WCF, so maybe this is something best done in another way.

Right now I have a collection of WCF Services, but I am trying to build in functionality which sends weekly emails. To do this I built another WCF service with the code below:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AutomaticSessionShutdown = false)]
public class TimerService : ITimerService
{
    private static Timer timer;
    private static TimeSpan tSpan = new TimeSpan(0, 0, 20, 0);
    private static OtherService Ref = new OtherService();

    public void ToggleEmailTimer(bool enabled)
    {
        if (enabled)
            timer = new Timer(new TimerCallback(TimerElapsed), null, tSpan, tSpan);
        else
        {   
            if(timer != null)
                timer.Dispose();
        }
    }

    private void TimerElapsed(object state)
    {
        Ref.SendWeekly();
    }
}

It begins disabled, and I enable it from an aspx page.For testing, I've managed to make this work for 10 minutes intervals, and it seems to break somewhere around setting the interval to 15 minutes.

To me, it seems like the WCF Serivce session is expiring from inactivity, which would explain why the timer just stops. Is there a way to specify the lifetime of the WCF Service so that I could enabled the timer from the aspx page, exit, and the timer service will persist? I have seen information about setting timeout values but it is still unclear to me if this applies.

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

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

发布评论

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

评论(2

仅一夜美梦 2024-09-18 08:07:51

其他人是正确的,您不想使用 WCF 进行实际的任务调度。虽然理论上可以摆弄应用程序池回收,但这绝对不是理想的途径。使用Windows任务调度将是一个很好的解决方案。实现此目的的最简单方法是通过 REST 公开 WCF 服务(通过在 .svc 文件中使用 Factory="System.ServiceModel.Activation.WebServiceHostFactory"。)这样,您就可以通过访问网址。

The others are correct that you do not want to use WCF for the actual task scheduling. While it is theoretically possible to fiddle with the app pool recycling, it is definitely not the ideal route. Using windows task scheduling would be a fine solution. The easiest way to accomplish this is to expose your WCF services via REST (by using Factory="System.ServiceModel.Activation.WebServiceHostFactory" in your .svc file.) That way, you can have Windows task scheduling invoke your service by accessing a URL.

薄暮涼年 2024-09-18 08:07:51

这很可能与主机进程有关,而不是与 WCF 实例上下文行为有关。只要主机进程允许,“单一”实例上下文就应该存在。但 IIS 默认情况下会根据各种情况回收工作进程。这些条件可能包括健康进程的最大生命周期,但健康检查期间的内存占用和延迟也可能导致 IIS 终止该进程。

虽然您可以使用 IIS 配置或创建想要长期运行的自定义主机进程,但这对于 WCF 来说根本不是一个好的方案。

对于应该每周运行的东西来说,即使是标准的 Windows 服务进程也显得有些过分了。为什么不直接使用 Windows 任务计划程序来计划任务呢?如果它是长期运行的工作流的一部分,也许您应该考虑研究 Windows Workflow Foundation。

This most likely has to do with the host process and not the WCF instance context behavior. A "Single" instance context should live for as long as the host process lets it. But IIS by default will recycle worker processes upon various conditions. These conditions can include a maximum lifetime of even a healthy process, but also memory footprint and latency during health checks can cause IIS to kill the process.

While you could play with the IIS configuration or create a custom host process that you intend to be long-running, this is not a good scenario for WCF at all.

Even a standard Windows Service process seems like overkill for something that is supposed to act weekly. Why not just schedule a task using the Windows Task Scheduler? If it's part of a long-running workflow, perhaps you should consider looking into Windows Workflow Foundation.

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