Search code examples
c#azuretimerwebjob

Azure C# Webjob TimeTrigger not firing


I'm attempting to create an Azure WebJob that will execute Functions.Methods on a set timer. I have setup a simple WebJob Project in VS2017 and added Microsoft.Azure.Webjobs.Extensions to allow me to reference Timers.

As a learning process, i'm attempting to create a WebJob that will call a Function Method to send an email to my address when the TimerTrigger Fires (send an email every 60 seconds for example)

My Main void looks like this:

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

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


        config.UseTimers();

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

The Function method i would like to call looks like this:

    [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");
    }

Now if i Debug the above locally i see this

The SendMail Method Never fires.

Now if I upload this as a WebJob to azure, then navigate to WebJob Dashboard/Functions/Function Invocation Log and "Run Function" I receive an e-mail shortly after from My SendMail Method with the following output:

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

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

RE: [NoAutomaticTrigger]

One suggestion I've had was to remove the [NoAutomaticTrigger] attribute from the SendMail Method as this tells the jobhost to ignore the timer but if I do this I get:

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.).

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

Thanks in advance


Solution

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

    Based on your code, I found that you missed to pass the config parameter to JobHost.

    var host = new JobHost(config);
    

    In addition, the NoAutomaticTrigger attribute did need to be removed and the TimerTrigger attribute should be applied to a parameter whose type is TimerInfo. Code below is for your reference.

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

    After that, the TimerTrigger will work.

    enter image description here