App-vNext/Polly · error · ArgumentNullException

Value cannot be null.

Error message

Value cannot be null.

What it means

Thrown as ArgumentNullException(nameof(onBulkheadRejected)) from the non-generic Policy.Bulkhead(maxParallelization, maxQueuingActions, onBulkheadRejected) overload when the rejection callback is null. The guard at BulkheadSyntax.cs:65–68 fires at construction time. The callback is invoked when the bulkhead rejects execution due to overflow.

Source

Thrown at src/Polly/Bulkhead/BulkheadSyntax.cs:65

    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentOutOfRangeException">maxParallelization;Value must be greater than zero.</exception>
    /// <exception cref="ArgumentOutOfRangeException">maxQueuingActions;Value must be greater than or equal to zero.</exception>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="onBulkheadRejected"/> is <see langword="null"/>.</exception>
    public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action<Context> onBulkheadRejected)
    {
        if (maxParallelization <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero.");
        }

        if (maxQueuingActions < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero.");
        }

        if (onBulkheadRejected == null)
        {
            throw new ArgumentNullException(nameof(onBulkheadRejected));
        }

        return new BulkheadPolicy(
            maxParallelization,
            maxQueuingActions,
            onBulkheadRejected);
    }

    private static void EmptyAction(Context context)
    {
        // No-op
    }
}

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Use the two-parameter Policy.Bulkhead(maxParallelization, maxQueuingActions) overload if no rejection callback is needed
  2. Pass a no-op lambda: _ => { } as the callback
  3. Ensure the callback variable is non-null before passing it

Example fix

// before
Action<Context> onReject = condition ? ctx => LogReject(ctx) : null;
var policy = Policy.Bulkhead(10, 5, onReject); // throws if condition is false

// after
var policy = Policy.Bulkhead(10, 5); // use two-parameter overload
// or:
Action<Context> onReject = condition ? ctx => LogReject(ctx) : _ => { };
var policy = Policy.Bulkhead(10, 5, onReject);
Defensive patterns

Strategy: validation

Validate before calling

Action<Context> safeOnReject = onBulkheadRejected ?? (_ => { });
var policy = Policy.Bulkhead(maxParallelization, maxQueuingActions, safeOnReject);

Type guard

public static bool IsNonNullCallback(Action<Context> callback) => callback is not null;

Prevention

When it happens

Trigger: Calling Policy.Bulkhead(maxParallelization, maxQueuingActions, null) — passing null as the onBulkheadRejected parameter. The guard at BulkheadSyntax.cs:65 fires before the BulkheadPolicy is constructed.

Common situations: Using the three-parameter overload without needing a rejection callback (use the two-parameter overload instead). Passing a callback variable that was conditionally assigned and ended up null.

Related errors


AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13). Data as JSON: /api/errors/17751ff2fd0871c0. Report an issue: GitHub.