Conditionally disable Azure Functions based on configuration values using attributes.
dotnet add package AzureFunctions.DisabledWhenSee sample/SampleFunctionApp for a working example.
dotnet add package AzureFunctions.DisabledWhen
dotnet add package AzureFunctions.DisabledWhen.SourceGeneratorAdd this to your .csproj to enable interceptors:
<PropertyGroup>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);AzureFunctions.DisabledWhen</InterceptorsPreviewNamespaces>
</PropertyGroup>See sample/SampleFunctionApp.SourceGenerated for a working example.
Note: The source generator only discovers functions in the same assembly where
UseDisabledWhen()is called. If your functions are spread across multiple assemblies, use the reflection-based package instead.
In your Program.cs, call UseDisabledWhen() on the builder after ConfigureFunctionsWebApplication():
using AzureFunctions.DisabledWhen;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.UseDisabledWhen();
var host = builder.Build();
host.Run();The legacy IHostBuilder pattern is also supported:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.UseDisabledWhen()
.Build();
host.Run();Important:
UseDisabledWhen()must be called afterConfigureFunctionsWebApplication()(orConfigureFunctionsWorkerDefaults()) to ensure the default function metadata provider is registered first.
using AzureFunctions.DisabledWhen;
public class MyFunctions
{
[Function("ScheduledCleanup")]
[DisabledWhenLocal]
public static void ScheduledCleanup(
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timer)
{
}
[Function("ProcessOrderQueue")]
[DisabledWhenNullOrEmpty("ServiceBusConnection")]
public static void ProcessOrderQueue(
[ServiceBusTrigger("orders", Connection = "ServiceBusConnection")] string message)
{
}
[Function("GdprDataExport")]
[DisabledWhen("EnvironmentOptions:RegionAbbreviation", "US", StringComparison.OrdinalIgnoreCase)]
[DisabledWhen("EnvironmentOptions:RegionAbbreviation", "CA", StringComparison.OrdinalIgnoreCase)]
[DisabledWhen("EnvironmentOptions:RegionAbbreviation", "AU", StringComparison.OrdinalIgnoreCase)]
public static IActionResult GdprDataExport(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "gdpr/export/{userId}")] HttpRequest req,
string userId)
{
}
}Disables a function when a configuration key matches a specific value.
[DisabledWhen("ConfigKey", "ConfigValue")]
[DisabledWhen("ConfigKey", "ConfigValue", StringComparison.OrdinalIgnoreCase)]Disables a function when AZURE_FUNCTIONS_ENVIRONMENT equals Development.
[DisabledWhenLocal]Read more about AZURE_FUNCTIONS_ENVIRONMENT.
Disables a function when a configuration key is missing, null, or empty.
[DisabledWhenNullOrEmpty("RequiredConfigKey")]The library provides a custom IFunctionMetadataProvider that evaluates attribute conditions at startup and excludes disabled functions from registration. The approach is inspired from this github issue.
When using both packages, the SourceGenerator intercepts calls to UseDisabledWhen() and replaces the reflection-based implementation with a compile-time generated version that is AOT-compatible.
Since filtering happens at startup:
- Configuration changes require a restart to take effect
- Disabled functions don't appear in the Azure Portal
- Disabled functions are logged as warnings
MIT