{"record":{"id":"898f9692dd11480c","repo":"App-vNext/Polly","slug":"value-cannot-be-null-parameter-context-898f96","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.TResult.ExecuteOverloads.cs","lineNumber":72,"sourceCode":"    /// <returns>The value returned by the action.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"contextData\"/> is <see langword=\"null\"/>.</exception>\n    [DebuggerStepThrough]\n    public TResult Execute(Func<Context, CancellationToken, TResult> 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 and returns the result.\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    /// <returns>The value returned by the action.</returns>\n    [DebuggerStepThrough]\n    public TResult Execute(Func<Context, CancellationToken, TResult> 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            return Implementation(action, context, cancellationToken);\n        }\n        finally\n        {\n            RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey);\n        }\n    }\n\n    #endregion\n\n    #region ExecuteAndCapture overloads\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Policy.TResult.ExecuteOverloads.cs#L54-L90","documentation":"Thrown by Policy<TResult>.Execute(Func<Context,CancellationToken,TResult>, Context, CancellationToken) when context is null. This is the strongly-typed (result-locked) policy variant; the guard protects the SetPolicyContext/RestorePolicyContext pair that stamps policy keys onto the context.","triggerScenarios":"Calling typedPolicy.Execute(func, null, cancellationToken) on a Policy<TResult> instance with a null Context reference.","commonSituations":"Migrating from a generic Policy to a Policy<TResult> and forgetting that the Context overload still requires a value; forwarding an optional Context parameter without defaulting; reflection-driven test harnesses.","solutions":["Pass a real Context: typedPolicy.Execute(func, new Context(), ct).","Default optional parameters: Context context = null is invalid; use Context context = new().","Add a precondition null check at the call site.","Enable NRTs so the compiler rejects Context? being passed to Context parameters."],"exampleFix":"// before\nvar r = typedPolicy.Execute(func, null, ct);\n\n// after\nvar r = typedPolicy.Execute(func, new Context(), ct);","handlingStrategy":"validation","validationCode":"Context ctx = providedContext ?? new Context();\nvar r = typedPolicy.Execute(func, ctx, ct);","typeGuard":"static bool IsContext(object o) => o is Context;","tryCatchPattern":null,"preventionTips":["When migrating to Policy<TResult>, remember Context-bearing overloads still require non-null context.","Default optional Context parameters to new Context().","Enable NRTs to reject Context? at call sites."],"tags":["argumentnullexception","context","validation","legacy-api","tresult"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}