2
votes

I have a solution in Visual Studio 2019, I added a new project of type "Azure functions". I have a function in that project that runs on a timer trigger.

I want to run that function only once during debug, is there a way to do that without fiddling with the timer settings so that it will only start, like, 10 seconds after I start debugging?

The function does not have any endpoints and it's not being called by anything, its point is to run on a schedule.

If I were to start debugging and set the timer to run it every X, but I want to run it explicitly only one time while debugging.

2

2 Answers

4
votes

You can use the RunOnStartup option inside the [TimerTrigger] attribute. For example:

public static void Run(
    [TimerTrigger("0 */5 * * * *", RunOnStartup = true)]TimerInfo myTimer, 
    ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

You can make it debug-only by using preprocessor directives:

public static void Run([TimerTrigger("0 */5 * * * *",
#if DEBUG
    RunOnStartup = true
#endif
    )]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
2
votes

You can execute timer triggered functions by sending a request to its admin url:

http://localhost:<your port>/admin/functions/<your function name>

Example POST:

POST /admin/functions/<function name> HTTP/1.1
Host: localhost:5800
x-functions-key: <your function key>
Content-Type: application/json

{}