{"record":{"id":"31b872dc9a2e1dc8","repo":"App-vNext/Polly","slug":"value-cannot-be-null-parameter-context","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'context')","messagePattern":"Value cannot be null\\. \\(Parameter 'context'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Policy.ExecuteOverloads.cs","lineNumber":64,"sourceCode":"    /// <param name=\"contextData\">Arbitrary data that is passed to the exception policy.</param>\n    /// <param name=\"cancellationToken\">The cancellation token.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"contextData\"/> is <see langword=\"null\"/>.</exception>\n    [DebuggerStepThrough]\n    public void Execute(Action<Context, CancellationToken> action, IDictionary<string, object> contextData, CancellationToken cancellationToken) =>\n        Execute(action, new Context(contextData), cancellationToken);\n\n    /// <summary>\n    /// Executes the specified action within the policy.\n    /// </summary>\n    /// <param name=\"action\">The action to perform.</param>\n    /// <param name=\"context\">Context data that is passed to the exception policy.</param>\n    /// <param name=\"cancellationToken\">The cancellation token.</param>\n    [DebuggerStepThrough]\n    public void Execute(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken)\n    {\n        if (context == null)\n        {\n            throw new ArgumentNullException(nameof(context));\n        }\n\n        SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey);\n\n        try\n        {\n            Implementation(action, context, cancellationToken);\n        }\n        finally\n        {\n            RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);\n        }\n    }\n\n    #region Overloads method-generic in TResult\n\n    /// <summary>\n    /// Executes the specified action within the policy and returns the Result.","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Policy.ExecuteOverloads.cs#L46-L82","documentation":"Thrown by Policy.Execute(Action<Context,CancellationToken>, Context, CancellationToken) when the context argument is null. The context carries per-execution metadata and policy wrap keys through the pipeline, so Polly refuses to run without it. The guard sits before SetPolicyContext so that context bookkeeping never dereferences null.","triggerScenarios":"Invoking policy.Execute(action, null, cancellationToken) on a non-generic Policy where the second argument is a null Context reference. Any synchronous Execute overload that requires an explicit Context hits this same guard shape.","commonSituations":"Passing a Context obtained from a nullable field or optional parameter without checking it; migrating from a context-free Execute overload and forgetting to construct a Context; DI/proxy code that forwards a possibly-null context.","solutions":["Construct and pass a new Context() (or Context with a correlation key) instead of null: policy.Execute(action, new Context(), ct).","If the context arrives from an optional parameter, default it to a fresh Context in the method signature.","Add a null check on the context at the call site before invoking Execute.","Enable nullable reference types so the compiler flags nullable Context arguments at the call site."],"exampleFix":"// before\nContext ctx = GetContextMaybeNull();\npolicy.Execute(action, ctx, ct);\n\n// after\nContext ctx = GetContextMaybeNull() ?? new Context();\npolicy.Execute(action, ctx, ct);","handlingStrategy":"validation","validationCode":"Context ctx = providedContext ?? new Context();\npolicy.Execute(action, ctx, cancellationToken);","typeGuard":"static bool IsContext(object o) => o is Context;\n// or guard: if (providedContext is not Context) return;","tryCatchPattern":null,"preventionTips":["Default optional Context parameters to new Context() rather than null.","Treat Context as non-nullable in your API surface; enable NRTs.","Audit context-producing helpers to ensure they never return null."],"tags":["argumentnullexception","context","validation","legacy-api"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}