【问题标题】:Set time for scheduled task via ASP.NET通过 ASP.NET 设置计划任务的时间
【发布时间】:2014-04-15 02:55:52
【问题描述】:

我必须解决以下问题。我们得到了每两分钟请求一次的 ASMX Web 服务。如果十分钟内未请求此服务,则应发送一封电子邮件。我们想通过使用计划任务来实现这一点。我们是这样想象的

1. Creating a scheduled task which will send an email every ten minutes
2. If the service is requested the execution time for the task will be set to ten minutes from now and so the execution time cannot be reached
- If the service is not requested the execution time will be reached and the email is sent

有没有办法在 ASP.NET 中解决这个问题,或者有更好的解决方案?

感谢您的回复。

【问题讨论】:

    标签: asp.net web-services email timer scheduled-tasks


    【解决方案1】:

    您可能想看看Revalee 开源项目。

    您可以使用它在特定时间安排网络回调。在您的情况下,您可以在每次使用 Web 服务时安排一次 Web 回调(未来 10 分钟)。当您的 Web 服务收到回调时,它可以确定该服务最近是否被使用过。如果 Web 服务已激活,则忽略回调;如果 Web 服务处于非活动状态,则它可以发送电子邮件消息。

    例如,使用 Revalee,您可以:

    1. 在您的应用程序启动时注册未来(从现在起 10 分钟)回调。

      private DateTimeOffet? lastActive = null;
      
      private void ScheduleTenMinuteCallback()
      {
          // Schedule your callback 10 minutes from now
          DateTimeOffset callbackTime = DateTimeOffset.Now.AddMinutes(10.0);
      
          // Your web service's Uri
          Uri callbackUrl = new Uri("http://yourwebservice.com/ScheduledCallback/YourActivityMonitor");
      
          // Register the callback request with the Revalee service
          RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
      }
      
    2. 无论何时使用您的网络服务,您都会注册另一个回调将您的服务处于活动状态的日期和时间存储为全局值。

      lastActive = DateTimeOffset.Now;
      
      ScheduleTenMinuteCallback();
      
    3. 最后,当 web schedule 任务激活并回调你的应用程序时,你测试全局的值

      private void YourActivityMonitor()
      {
          if (!lastActive.HasValue || lastActive.Value <= DateTimeOffset.Now.AddMinutes(-10.0))
          {
              // Send your "10 minutes has elapsed" email message
          }
      }
      

    我希望这会有所帮助。

    免责声明:我是参与 Revalee 项目的开发人员之一。然而,需要明确的是,Revalee 是免费的开源软件。源代码可在 GitHub 上获得。

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 2011-02-25
      • 2016-11-08
      • 2012-11-24
      • 2012-11-14
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      相关资源
      最近更新 更多