Disable Property of Azure Functions not working in Visual Studio 2017
Disable properties default values is true
.
Use Disable()
instead of Disable("true")
.
So the code will look like
public static void Run([TimerTrigger("0 */15 * * * *"), Disable()]TimerInfo myTimer, TraceWriter log)
.
If you want to enable the function use Disable("False")
.
I wanted to add this answer, as I have been searching for it as well and I believe I found a good way to disable functions for debugging/development purposes (and to avoid these local changes get into the deployment pipelines/source control).
I combine the #if DEBUG
statements with the Disable(string SettingsName)
attribute:
The following piece of code shows different things at work:
- The Disable attribute is not using the parameter to indicate True or False as a value, but it refers to an appsetting (that you can put in the local.settings.json file). See the second snippet in this post. So, just by changing that settings in the appsettings file, I can enable and disable triggers easily, without impacting my git repo or deployment pipelines.
- The other interesting thing is the
RunOnStartup = true
parameter when using aTimerTrigger
. This one (which I only enable with the DEBUG compiler directives), will allow me to trigger the timer function immediately, without waiting for the next CRON cycle to happen. (an alternative would be to do a post to your local functions endpoint, as described in this stackoverflow post). So again, when assuming you run your production workloads in the RELEASE configuration, this is only impacting your local development environment and not your development team or releases.
1st snippet (attributes)
#if DEBUG
[Disable("disable-transactioncrawler")]
#endif
[FunctionName("TransactionCrawler")]
public async Task Run([TimerTrigger("50 */10 * * * *"
#if DEBUG
, RunOnStartup = true
#endif
)]TimerInfo myTimer, ILogger log)
{
// Logic comes here
}
2nd snippet (local.appsettings.json)
{
"Values":
{
"disable-transactioncrawler": false
}
}
The following 'solutions' typically impact your production code, which can lead to issues:
- Just using
Disable()
is not allowing you to configure/change it afterwards - Updating the host.json file to only include the triggers you want to run is also risking to have this change arriving in production.
Functions 2.x can be disabled individually via local.settings.json
in the following manner
{
"IsEncrypted": false,
"Values": {
"AzureWebJobs.MyFunctionNameOne.Disabled": "true",
"AzureWebJobs.MyFunctionNameTwo.Disabled": "true",
...
}
}
Ref: https://docs.microsoft.com/en-us/azure/azure-functions/disable-function#functions-2x---all-languages