Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set RunOnStartup in Azure Function?

Tags:

c#

.net

azure

I'm creating an Azure function. While testing on my localhost, I'd like it to execute immediately. But in Prod, it can run every 5 minutes. I'd like to not have to rely on humans to remember to make this change.

public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)])

I've been playing around with various ways to make the true here somehow variable, but have not found a solution. I was thinking something like:

public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = #DEBUG ? true : false)])

But inline #DEBUG is not allowed.

like image 738
Casey Crookston Avatar asked Sep 02 '26 03:09

Casey Crookston


1 Answers

For better readability, you can define a constant bool that denotes whether you're running a DEBUG build:

#if DEBUG
    const bool IS_DEBUG = true;
#else
    const bool IS_DEBUG = false;
#endif

Then use it in your attribute:

public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = IS_DEBUG)])
like image 110
haim770 Avatar answered Sep 04 '26 15:09

haim770



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!