Azure C# Webjob TimeTrigger 未触发

Azure C# Webjob TimeTrigger not firing

我正在尝试创建一个 Azure WebJob,它将在设置的计时器上执行 Functions.Methods。我在 VS2017 中设置了一个简单的 WebJob 项目并添加了 Microsoft.Azure.Webjobs.Extensions 以允许我引用计时器。

作为一个学习过程,我正在尝试创建一个 WebJob,它会在 TimerTrigger 触发时调用函数方法向我的地址发送一封电子邮件(例如,每 60 秒发送一封电子邮件)

我的 Main void 看起来像这样:

public static void Main()
    {
        var config = new JobHostConfiguration();

        config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Verbose;


        config.UseTimers();

        var host = new JobHost();
        host.RunAndBlock();
    }

我想调用的函数方法如下所示:

    [NoAutomaticTrigger]    //See below note on [NoAutomaticTrigger]
    public static void SendMail([TimerTrigger("00:00:10", RunOnStartup = true)] TextWriter log)
    {
        log.WriteLine("[" + DateTime.Now.ToString() + "] Preparing to send Mail");
        MailMessage Message = new MailMessage();
        SmtpClient MailClient = new SmtpClient();

        String FromAddress = "****";
        String FromName = "****";
        String Password = "****;
        String ToAddress = "****";

        MailClient.UseDefaultCredentials = false;
        MailClient.Credentials = new System.Net.NetworkCredential(FromAddress, Password);
        MailClient.Port = 587;
        MailClient.Host = "****";
        MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        MailClient.EnableSsl = true;



        Message.To.Add(ToAddress);
        Message.From = new MailAddress(FromAddress, FromName);
        Message.Subject = "WebJob | Test Mail";
        Message.Body = "TimerTrigger Function Sendmail triggered at: " + DateTime.Now.ToString();
        MailClient.Send(Message);
        log.WriteLine("[" + DateTime.Now.ToString() + "] Mail Successfully Sent");
    }

现在,如果我在本地调试上面的内容,我会看到 this

SendMail 方法从不触发。

现在,如果我将其作为 WebJob 上传到 Azure,然后导航到 WebJob Dashboard/Functions/Function 调用日志和“运行 函数”,不久之后我会从我的 SendMail 方法中收到一封电子邮件,其中包含以下输出:

[5/20/2017 11:47:31 AM] Preparing to send Mail

[5/20/2017 11:47:32 AM] Mail Successfully Sent

回复:[NoAutomaticTrigger]

我的一个建议是从 SendMail 方法中删除 [NoAutomaticTrigger] 属性,因为这会告诉作业主机忽略计时器,但如果我这样做,我会得到:

No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

所以问题是,当 运行 在 Azure 中作为 WebJob

时,如何让我的 sendmail 方法在指定的 [TimerTrigger] 上触发

提前致谢

So the question is, how can I make my sendmail method fire on the specified [TimerTrigger] when running in Azure as a WebJob

根据您的代码,我发现您没有将配置参数传递给 JobHost。

var host = new JobHost(config);

此外,确实需要删除 NoAutomaticTrigger 属性,并且应该将 TimerTrigger 属性应用于类型为 TimerInfo 的参数。以下代码供您参考。

public static void SendMail([TimerTrigger("00:00:10")] TimerInfo timer, TextWriter log)

之后,TimerTrigger 将起作用。