AzureFunctions.DisabledWhen 1.0.0-prerelease

This is a prerelease version of AzureFunctions.DisabledWhen.
dotnet add package AzureFunctions.DisabledWhen --version 1.0.0-prerelease
                    
NuGet\Install-Package AzureFunctions.DisabledWhen -Version 1.0.0-prerelease
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureFunctions.DisabledWhen" Version="1.0.0-prerelease" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AzureFunctions.DisabledWhen" Version="1.0.0-prerelease" />
                    
Directory.Packages.props
<PackageReference Include="AzureFunctions.DisabledWhen" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AzureFunctions.DisabledWhen --version 1.0.0-prerelease
                    
#r "nuget: AzureFunctions.DisabledWhen, 1.0.0-prerelease"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AzureFunctions.DisabledWhen@1.0.0-prerelease
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AzureFunctions.DisabledWhen&version=1.0.0-prerelease&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=AzureFunctions.DisabledWhen&version=1.0.0-prerelease&prerelease
                    
Install as a Cake Tool

AzureFunctions.DisabledWhen

Conditionally disable Azure Functions based on configuration values using attributes.

Packages

Package Description
AzureFunctions.DisabledWhen Reflection-based implementation
AzureFunctions.DisabledWhen.SourceGenerator Source-generated implementation

Installation

Reflection-based (simple setup)

dotnet add package AzureFunctions.DisabledWhen

See sample/SampleFunctionApp for a working example.

Source-generated (AOT-compatible)

dotnet add package AzureFunctions.DisabledWhen
dotnet add package AzureFunctions.DisabledWhen.SourceGenerator

Add 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.

Usage

1. Register the metadata provider

In your Program.cs, call UseDisabledWhen() on the host builder after ConfigureFunctionsWebApplication():

using AzureFunctions.DisabledWhen;

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .UseDisabledWhen() // Must be called after ConfigureFunctionsWebApplication
    .Build();

host.Run();

Important: UseDisabledWhen() must be called after ConfigureFunctionsWebApplication() (or ConfigureFunctionsWorkerDefaults()) to ensure the default function metadata provider is registered first.

2. Apply attributes to functions

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)
    {
    }
}

Attributes

DisabledWhenAttribute

Disables a function when a configuration key matches a specific value.

[DisabledWhen("ConfigKey", "ConfigValue")]
[DisabledWhen("ConfigKey", "ConfigValue", StringComparison.OrdinalIgnoreCase)]

DisabledWhenLocalAttribute

Disables a function when AZURE_FUNCTIONS_ENVIRONMENT equals Development.

[DisabledWhenLocal]

Read more about AZURE_FUNCTIONS_ENVIRONMENT.

DisabledWhenNullOrEmptyAttribute

Disables a function when a configuration key is missing, null, or empty.

[DisabledWhenNullOrEmpty("RequiredConfigKey")]

How It Works

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

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-prerelease 44 1/21/2026