{"record":{"id":"bd81f2aca9f64962","repo":"JeffreySu/WeiXinMPSDK","slug":"httpcontext","errorCode":null,"errorMessage":"httpContext","messagePattern":"httpContext","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs","lineNumber":132,"sourceCode":"        }\n\n        /// <summary>\n        /// 使用自定义请求体上限的兼容构造函数。新代码优先使用 <see cref=\"CreateAsync\"/>。\n        /// </summary>\n        public TenPayNotifyHandler(HttpContext httpContext, ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3, int maxBodyBytes)\n            : this(\n                httpContext,\n                senparcWeixinSettingForTenpayV3,\n                ReadBodyAsync(httpContext, maxBodyBytes, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult())\n        {\n        }\n\n        private TenPayNotifyHandler(\n            HttpContext httpContext,\n            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3,\n            NotificationBody notificationBody)\n        {\n            _ = httpContext ?? throw new ArgumentNullException(nameof(httpContext));\n            _httpContext = httpContext;\n            _tenpayV3Setting = senparcWeixinSettingForTenpayV3 ?? Senparc.Weixin.Config.SenparcWeixinSetting.TenpayV3Setting;\n\n            if (!_tenpayV3Setting.EncryptionType.HasValue)\n            {\n                throw new Senparc.Weixin.Exceptions.WeixinException(\"没有设置证书加密类型（EncryptionType）\");\n            }\n\n            Body = notificationBody.Body;\n            NotifyRequest = notificationBody.NotifyRequest;\n        }\n\n        /// <summary>\n        /// 异步创建通知处理器，支持请求取消并限制请求体大小。\n        /// </summary>\n        public static async Task<TenPayNotifyHandler> CreateAsync(\n            HttpContext httpContext,\n            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null,","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs#L114-L150","documentation":"The private TenPayNotifyHandler constructor validates its httpContext argument and throws ArgumentNullException naming \"httpContext\" when it is null. The HttpContext is required for reading request headers and body during payment notification processing.","triggerScenarios":"Calling the factory/method that creates TenPayNotifyHandler while passing a null HttpContext (e.g. outside an HTTP request pipeline, or in unit tests with no HttpContext).","commonSituations":"Invoking notify-handler logic from background jobs or console code where there is no current HttpContext; tests constructing the handler manually.","solutions":["Only create TenPayNotifyHandler inside an active ASP.NET Core request (e.g. from an MVC/API controller action)","Pass IHttpContextAccessor.HttpContext only after checking it is not null","In tests, supply a valid DefaultHttpContext instance","Add an early null check on httpContext before calling the factory"],"exampleFix":"// before\nvar handler = TenPayNotifyHandler.Create(httpContext, settings);\n// after\nif (httpContext == null) throw new InvalidOperationException(\"TenPayNotifyHandler 只能在 HTTP 请求上下文中使用。\");\nvar handler = TenPayNotifyHandler.Create(httpContext, settings);","handlingStrategy":"type-guard","validationCode":"if (HttpContext is null) return BadRequest(\"必须在 HTTP 请求上下文中处理支付通知。\");","typeGuard":"bool HasHttpContext(HttpContext? ctx) => ctx is not null;","tryCatchPattern":"try { var handler = TenPayNotifyHandler.Create(httpContext, settings); ... }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"httpContext\") { return StatusCode(500, \"缺少 HttpContext\"); }","preventionTips":["Only instantiate the notify handler inside controller actions","Avoid capturing HttpContext in background threads (use IHttpContextAccessor carefully)","Use DefaultHttpContext in tests"],"tags":["null-argument","aspnetcore","tenpay"],"backgroundTag":"null-argument","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}