I am working on Azure functions timer Job , i need to get the cron expression from the appsettings. Please let me know, how can i get the value from the appsettings in the Azure functions. I want to run my azure function starting from 9:00 AM to 12:00 PM for every 30 minutes\
{
"disabled": false,
"bindings": [
{
"name": "timerInfo",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 * * * * *"
}
]
}
To find the application settings, see Get started in the Azure portal. The Application settings tab maintains settings that are used by your function app. You must select Show values to see the values in the portal. To add a setting in the portal, select New application setting and add the new key-value pair.
Create a timer triggered functionIn your function app, select Functions, and then select + Create. Select the Timer trigger template. Configure the new trigger with the settings as specified in the table below the image, and then select Create. Defines the name of your timer triggered function.
Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.
Set your schedule as "schedule": "%EmailScheduleTriggerTime%"
and then in the appsetting.json or local.settings.json you can set EmailScheduleTriggerTime value as "0 30 9-12 * * *"
{
"IsEncrypted": false,
"Values": {
"EmailScheduleTriggerTime": "0 30 9-12 * * *", //Run every 30 minutes from 9:00 to 12:00
},
"ConnectionStrings": {
"DefaultConnection": ""
}
}
[FunctionName("TimerfunctionApp")]
public static void Run([TimerTrigger("%EmailScheduleTriggerTime%")] TimerInfo TInfo, TraceWriter log)
If you are using the VS2017 Functions tooling and defining your function in a .NET project (rather than directly in the Azure portal) you can pick up the interval from AppSettings
using the %
syntax:
[FunctionName("MyTimerFunction")]
public static void Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, TraceWriter log, ..
Then in your app settings specify the required CRON format interval eg. in local.settings.json
{
"Values" : {
"TimerInterval" : "0 30 9-12 * * *"
}
}
To add to the previous answers, you can get any value form any field in a config file (appsettings.json
) file using %
syntax - not only from Values
configuration object.
For example:
appsettings.json
:
{
"ScheduleConfiguration": {
"FunctionOne": {
"CronExpression": "0 40 2 * * *"
}
}
}
Functions.cs
:
/// <summary>
/// %ScheduleConfiguration:FunctionOne:CronExpression%
/// - e.g. "0 40 2 * * *" - executes at 02:40:00am every day
/// </summary>
[FunctionName("FunctionOne")]
public async Task FunctionOne(
[TimerTrigger("%ScheduleConfiguration:FunctionOne:CronExpression%")]
TimerInfo timerInfo)
{
// Azure function code
}
Since this post already has good answers, I want to share my experience here. I was trying to run the app locally by adding the cron expression in the appsettings.json
file but then when I ran the function app I always received an error as follows
The 'EmployeeTimerTrigger' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'EmployeeTimerTrigger'. Microsoft.Azure.WebJobs.Host: '%EmployeeTimerTrigger%' does not resolve to a value.
So in order to resolve that what we need to do is shift the cron expression from appsettings.json
to local.settings.json
and it worked just fine and I was able to test it locally.
The function app reads the timer trigger settings from the application settings of the function app itself on azure.
When you plan to deploy your function app in azure there's a small change that you need to do in the Application Settings of the function app in the Azure portal.
You can navigate to those settings by selecting the function app in azure portal -> Configuration -> Application setting
The very first setting that you need to add is the time zone in which you want the timer trigger to run here's an example of the same
Note: Please note these values are case sensitive
Now the second setting we will add here is for our timer trigger function, in this image you will see that the timer will run at 10:00 AM and 2:00 PM eastern standard time.
Why eastern standard time you ask? well, remember our first settings were for the website time zone so both settings work hand in hand.
Note: The name of the timer trigger should match the one you have in your function app, please note these values are case sensitive and should match exactly with what you have in your timer trigger function.
Once you have added these settings, don't forget to save your changes and now you are all set to deploy your function app.
I hope this answer helps in the deployment and running the timer function locally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With