I have a few C# Azure Functions that run on a schedule using timer triggers. I've set them up like so, where %TimerSchedule%
refers to a cron expression in the app settings:
public static void Run([TimerTrigger("%TimerSchedule%")]TimerInfo myTimer, TraceWriter log)
During development, I often want to run the functions locally using Azure Functions Tools for Visual Studio + Azure Functions Core Tools. But when I hit F5 to debug the function locally it (usually) doesn't run immediately. Instead, it starts waiting for the next occurrence as per the timer schedule. So for example, if my cron expression says to run daily at 8PM, I'd have to wait until 8PM for the function to actually run on my machine.
So my question is: What is the simplest and best way to make a function run once locally?
Things I have tried or considered:
Run()
method TimerInfo
and TraceWriter
arguments to Run()
– and I've found surprisingly little documentation for that. Microsoft's Strategies for testing your code in Azure Functions page is not very helpful on this topic – it only mentions timer triggers as a way to test other trigger types.
In a perfect world, I'd hit F5 and the function would immediately run once – just like developing a "normal" .NET app.
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.
Go to the Azure icon in the Activity bar. Under Local Project , find the function you want to run, right click, and select "Execute Function Now".
Copy the settings file to the root of the Functions project next to the DLL, open a command prompt from there, and run “func start.” The Function will run based off the same trigger it would use if hosted in Azure or it can be manually triggered for testing using a local HTTP endpoint.
I had the same question, and used the DEBUG-flag to have the RunOnStartup only while debugging:
public static void Run( [TimerTrigger("* 0 7 * * 1-5" #if DEBUG , RunOnStartup=true #endif )]TimerInfo myTimer, TraceWriter log) {
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