Azure webjobs:在计划的时间发送通知电子邮件吗?

时间:2019-01-08 05:03:57

标签: azure azure-webjobs azure-webjobssdk azure-webjobs-continuous

我需要一些有关如何使用天蓝色webjobs来满足此要求的反馈。

我有一个要求,我们需要在计划的时间发送通知电子邮件。通知的频率存储在数据库表中。表条目示例: enter image description here

Webjob应该从数据库表中读取配置,并在配置的时间(配置时间是数据库条目中提到的时间)发送通知电子邮件。 DB表中将有多个条目,并且可以添加或修改新条目。上面的示例只有2个条目,可以有更多条目。

问题:

  1. 从数据库中读取配置后,如何在一天的特定时间使webjob触发电子邮件?
  2. 是否有更好的方法来使用其他天蓝色资源来实施此通知调度?
  3. 如何确保网络作业成功发送了电子邮件?
  4. 如果Webjob无法发送电子邮件,是否有重试机制?

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果要从表中选择“ cron”数据然后创建计时器触发器,则可以使用表达式绑定。通过官方an overview of Azure SQL Database security capabilities,我们知道我们可以从应用设置中获取绑定,您可以这样做或参考下面的代码。

 public class Program
{
  public static void Main()
   {
    JobHostConfiguration config = new JobHostConfiguration();
    config.NameResolver = new TimeResolver();
    config.UseTimers();
    JobHost host = new JobHost(config);
    host.RunAndBlock();
}

private class TimeResolver : INameResolver
{
    public string Resolve(string name)
    {
        string value = string.Empty;
        switch (name)
        {
            case "TimerJob":
                Console.WriteLine("Name Is TimerJob : " + name);
                value = "00:00:10";
                break;
            case "TimerJobAlter":
                Console.WriteLine("Name Is TimerJobAlter : " + name);
                value = "00:00:20";
                break;
        }
        return value;
    }
}


//Runs once every 30 seconds
public static void TimerJob([TimerTrigger("%TimerJob%")] TimerInfo timer)
{
    Console.WriteLine("Timer1 job fired!");
}

// Runs once every 60 seconds
public static void TimerJobAlter([TimerTrigger("%TimerJobAlter%")] TimerInfo timer)
{
    Console.WriteLine("Timer2 job fired!");
}
}

您可以选择将值设置为配置文件,然后从中读取。关于阅读方法,您可以浏览此doc

关于使用表数据创建webjob的详细示例代码,抱歉,我没有,希望这些代码可以为您提供帮助。如果您还有其他问题,请告诉我。