Search code examples
azure-functions.net-core-3.1timer-trigger

Schedule a timer-triggered Azure function from the Azure Portal


I have an Azure Function (built in Visual Studio 2019, and running on the .NET Core 3.x stack in Azure) that is supposed to be triggered by a timer to run e.g. once per night.

I can easily create the function and define the schedule as an NCRONTAB expression like this:

[FunctionName("MyFunctionName")]
public void Run([TimerTrigger("0 15 3 * * 1-5")]TimerInfo myTimer, ILogger log)
{
    // Azure function code here ....
}

Works like a charm - BUT I'd like to be able to define the schedule in the Azure Portal - not in my function code - to handle e.g. DEV vs. TEST vs. PROD situations. On the DEV and TEST platforms, I might want to run this several times a day - in production maybe only once a week.

But with this approach, once the schedule is set - it's set, I cannot change it in the Azure Portal (all the input fields are grayed out / editing is disabled).

I was hoping I might be able to just skip the NCRONTAB expression in the declaration of my Azure function - like so:

[FunctionName("MyFunctionName")]
public void Run([TimerTrigger()]TimerInfo myTimer, ILogger log)
{
    // Azure function code here ....
}

and then specify the actual CRON expression in the Azure portal - but no go, the CRON expression is mandatory....

So am I asking for too much here? Or is there a way to schedule this outside of my actual code base?


Solution

  • As the post provided by silent, we can use ConfigurationManager.AppSettings["key"]. But I think you can refer to the solution below which is more simple.

    1. Edit your function code in local, do not specify a cron expression in the code, use %myTimerCron% instead.

    enter image description here

    2. Publish it from local to azure, and then add a key/value in the "Application settings". enter image description here

    3. Then you can edit the cron expression by edit the value of myTimerCron in "Application settings" on azure portal.

    By the way, when you test the code in local in visual studio, you just need to provide a cron expression with the name of "myTimerCron" in "local.setting.json". enter image description here