{"record":{"id":"d1d2742e3aa5124c","repo":"JeffreySu/WeiXinMPSDK","slug":"0-timeout-infinite-tenpayhttpclient","errorCode":null,"errorMessage":"超时时间必须大于 0，或使用 Timeout.Infinite。 ","messagePattern":"超时时间必须大于 0，或使用 Timeout\\.Infinite。 ","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs","lineNumber":157,"sourceCode":"        }\n\n        public Task<T> SendAsync<T>(string url, object data, int timeOut = Senparc.Weixin.Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func<T> createDefaultInstance = null)\n                   where T : ReturnJsonBase/*, new()*/\n        {\n            return SendAsync(url, data, CancellationToken.None, timeOut, requestMethod, checkSign, createDefaultInstance);\n        }\n\n        public async Task<T> SendAsync<T>(string url, object data, CancellationToken cancellationToken, int timeOut = Senparc.Weixin.Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func<T> createDefaultInstance = null)\n                   where T : ReturnJsonBase/*, new()*/\n        {\n            T result = null;\n            HttpMethod method = GetHttpMethod(requestMethod);\n\n            try\n            {\n                if (timeOut <= 0 && timeOut != Timeout.Infinite)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(timeOut), \"超时时间必须大于 0，或使用 Timeout.Infinite。 \");\n                }\n\n                using var request = new HttpRequestMessage(method, url);\n                SetRequestHeaders(request);\n\n                //设置请求 Json 字符串\n                string jsonString = data != null\n                    ? data.ToJson(false, RequestJsonSerializerSettings)\n                    : \"\";\n                WeixinTrace.SendApiPostDataLog(url, jsonString); //记录Post的Json数据\n                request.Content = new StringContent(jsonString, Encoding.UTF8, mediaType: \"application/json\");\n\n                // 进行签名\n                var authorization = await GenerateAuthorizationHeader(request);\n                request.Headers.Add(\"Authorization\", $\"WECHATPAY2-{_signer.GetAlgorithm()} {authorization}\");\n\n                // 发送请求\n                using var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs#L139-L175","documentation":"SendAsync validates the timeOut parameter before issuing the HTTP request: it must be strictly greater than 0 or exactly Timeout.Infinite (-1). Any other value (0, negative other than -1) causes ArgumentOutOfRangeException naming timeOut.","triggerScenarios":"Calling SendAsync (or any wrapper such as SendAsync that forwards a timeout) with timeOut = 0, a negative value like -1000, or an uninitialized/zero default from calling code.","commonSituations":"Passing 0 meaning 'no timeout' (wrong assumption); reading a timeout from config where the key is missing so the value defaults to 0; computing a timeout that evaluates to 0 after conversion of seconds/minutes.","solutions":["Pass a positive millisecond timeout, or System.Threading.Timeout.Infinite (-1) for no timeout.","Guard config-sourced values: default to a sane value (e.g. 30000 ms) when the configured timeout is <= 0.","Check the caller that computes the timeout to ensure the unit conversion yields milliseconds > 0."],"exampleFix":"// before\nawait client.SendAsync(requestMethod, url, timeOut: 0);\n// after\nawait client.SendAsync(requestMethod, url, timeOut: 30_000); // or Timeout.Infinite","handlingStrategy":"validation","validationCode":"if (timeOut <= 0 && timeOut != Timeout.Infinite)\n    timeOut = 30_000; // sane default before calling SendAsync","typeGuard":null,"tryCatchPattern":"try { await client.SendAsync(method, url, timeOut); }\ncatch (ArgumentOutOfRangeException) { logger.LogError(\"Invalid timeout {TimeOut}\", timeOut); }","preventionTips":["Centralize timeout constants; never pass 0 to mean 'no timeout' — use Timeout.Infinite.","Clamp config-sourced timeouts to a minimum positive value.","Watch unit conversions (seconds vs milliseconds)."],"tags":["argument","timeout","http","wechat-pay"],"backgroundTag":"argument-out-of-range","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"}