{"record":{"id":"982d81dc23870170","repo":"EllanJiang/GameFramework","slug":"you-must-add-web-request-agent-first","errorCode":null,"errorMessage":"You must add web request agent first.","messagePattern":"You must add web request agent first\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/WebRequest/WebRequestManager.cs","lineNumber":416,"sourceCode":"        /// <summary>\n        /// 增加 Web 请求任务。\n        /// </summary>\n        /// <param name=\"webRequestUri\">Web 请求地址。</param>\n        /// <param name=\"postData\">要发送的数据流。</param>\n        /// <param name=\"tag\">Web 请求任务的标签。</param>\n        /// <param name=\"priority\">Web 请求任务的优先级。</param>\n        /// <param name=\"userData\">用户自定义数据。</param>\n        /// <returns>新增 Web 请求任务的序列编号。</returns>\n        public int AddWebRequest(string webRequestUri, byte[] postData, string tag, int priority, object userData)\n        {\n            if (string.IsNullOrEmpty(webRequestUri))\n            {\n                throw new GameFrameworkException(\"Web request uri is invalid.\");\n            }\n\n            if (TotalAgentCount <= 0)\n            {\n                throw new GameFrameworkException(\"You must add web request agent first.\");\n            }\n\n            WebRequestTask webRequestTask = WebRequestTask.Create(webRequestUri, postData, tag, priority, m_Timeout, userData);\n            m_TaskPool.AddTask(webRequestTask);\n            return webRequestTask.SerialId;\n        }\n\n        /// <summary>\n        /// 根据 Web 请求任务的序列编号移除 Web 请求任务。\n        /// </summary>\n        /// <param name=\"serialId\">要移除 Web 请求任务的序列编号。</param>\n        /// <returns>是否移除 Web 请求任务成功。</returns>\n        public bool RemoveWebRequest(int serialId)\n        {\n            return m_TaskPool.RemoveTask(serialId);\n        }\n\n        /// <summary>","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/WebRequest/WebRequestManager.cs#L398-L434","documentation":"WebRequestManager.AddWebRequest throws GameFrameworkException with 'You must add web request agent first.' when a request is submitted while TotalAgentCount <= 0, i.e. no web request agent (and thus no helper) has been registered with the manager. The manager has no worker to process the task, so it refuses to enqueue it.","triggerScenarios":"Calling AddWebRequest before calling AddAgent (e.g. before the WebRequestComponent's Ready/agent setup runs, or in code executing during scene load before initialization).","commonSituations":"Game logic issues requests in Awake/OnEnable before the WebRequestComponent initializes agents; the agent-registration code was removed or gated behind a failed condition; a wrapper manager calls AddWebRequest before its own Init.","solutions":["Register at least one agent before any request: call m_WebRequestManager.AddAgent(typeof(UnityWebRequestAgentHelper)) (or let WebRequestComponent initialize it) prior to AddWebRequest.","Defer request-issuing code until after manager initialization (e.g. subscribe to a Start/Ready event).","Guard with a check: if (m_WebRequestManager.TotalAgentCount <= 0) initialize agents before submitting tasks."],"exampleFix":"// before\npublic void Awake()\n{\n    m_WebRequestManager.AddWebRequest(url); // no agent yet\n}\n// after\npublic void Start()\n{\n    if (m_WebRequestManager.TotalAgentCount <= 0)\n    {\n        m_WebRequestManager.AddAgent(typeof(UnityWebRequestAgentHelper), 1);\n    }\n    m_WebRequestManager.AddWebRequest(url);\n}","handlingStrategy":"validation","validationCode":"if (m_WebRequestManager.TotalAgentCount <= 0)\n{\n    m_WebRequestManager.AddAgent(typeof(UnityWebRequestAgentHelper), 1);\n}\nint serialId = m_WebRequestManager.AddWebRequest(webRequestUri);","typeGuard":null,"tryCatchPattern":"try\n{\n    int serialId = m_WebRequestManager.AddWebRequest(webRequestUri);\n}\ncatch (GameFrameworkException ex) when (ex.Message == \"You must add web request agent first.\")\n{\n    Log.Error(\"WebRequestManager not initialized: call AddAgent before AddWebRequest.\");\n}","preventionTips":["Initialize all managers (agents included) in a single bootstrap step before gameplay code runs.","Issue requests from Start or later, never in Awake of components that load before the WebRequestComponent.","Expose a safe wrapper around AddWebRequest that lazily ensures TotalAgentCount > 0."],"tags":["csharp","gameframework","webrequest","initialization-order","agent"],"backgroundTag":"module-init-failed","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}