{"record":{"id":"a758c2116c884255","repo":"EllanJiang/GameFramework","slug":"already-exist-network-channel-0","errorCode":null,"errorMessage":"Already exist network channel '{0}'.","messagePattern":"Already exist network channel '(.+?)'\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Network/NetworkManager.cs","lineNumber":239,"sourceCode":"        /// <param name=\"name\">网络频道名称。</param>\n        /// <param name=\"serviceType\">网络服务类型。</param>\n        /// <param name=\"networkChannelHelper\">网络频道辅助器。</param>\n        /// <returns>要创建的网络频道。</returns>\n        public INetworkChannel CreateNetworkChannel(string name, ServiceType serviceType, INetworkChannelHelper networkChannelHelper)\n        {\n            if (networkChannelHelper == null)\n            {\n                throw new GameFrameworkException(\"Network channel helper is invalid.\");\n            }\n\n            if (networkChannelHelper.PacketHeaderLength < 0)\n            {\n                throw new GameFrameworkException(\"Packet header length is invalid.\");\n            }\n\n            if (HasNetworkChannel(name))\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Already exist network channel '{0}'.\", name ?? string.Empty));\n            }\n\n            NetworkChannelBase networkChannel = null;\n            switch (serviceType)\n            {\n                case ServiceType.Tcp:\n                    networkChannel = new TcpNetworkChannel(name, networkChannelHelper);\n                    break;\n\n                case ServiceType.TcpWithSyncReceive:\n                    networkChannel = new TcpWithSyncReceiveNetworkChannel(name, networkChannelHelper);\n                    break;\n\n                default:\n                    throw new GameFrameworkException(Utility.Text.Format(\"Not supported service type '{0}'.\", serviceType));\n            }\n\n            networkChannel.NetworkChannelConnected += OnNetworkChannelConnected;","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Network/NetworkManager.cs#L221-L257","documentation":"NetworkManager keys channels by name and does not allow duplicates. CreateNetworkChannel throws this formatted error when a channel with the same name already exists. The caller must dispose the old channel or choose a unique name.","triggerScenarios":"Calling CreateNetworkChannel twice with the same name string without calling DestroyNetworkChannel in between, e.g. on reconnect logic or scene reload that re-runs initialization.","commonSituations":"Reconnect handlers that rebuild channels without tearing down the previous one; duplicate initialization from both Start and OnEnable; name collisions when opening multiple connections built from a shared naming template.","solutions":["Check HasNetworkChannel(name) before creating and reuse or destroy the existing channel","Call DestroyNetworkChannel(name) before re-creating","Generate unique names, e.g. append a counter or connection id"],"exampleFix":"// before\nvar channel = networkManager.CreateNetworkChannel(\"game\", ServiceType.Tcp, helper);\n// after\nif (networkManager.HasNetworkChannel(\"game\")) { networkManager.DestroyNetworkChannel(\"game\"); }\nvar channel = networkManager.CreateNetworkChannel(\"game\", ServiceType.Tcp, helper);","handlingStrategy":"validation","validationCode":"if (networkManager.HasNetworkChannel(name)) networkManager.DestroyNetworkChannel(name);\nvar channel = networkManager.CreateNetworkChannel(name, serviceType, helper);","typeGuard":null,"tryCatchPattern":"try { var channel = networkManager.CreateNetworkChannel(name, serviceType, helper); }\ncatch (GameFrameworkException ex) { Log.Error(\"Channel '{0}' already exists\", name); }","preventionTips":["Centralize channel creation in one idempotent helper method","Always tear down channels on scene unload/reconnect","Use unique names per connection"],"tags":["network","duplicate-key","resource-already-exists","gameframework"],"backgroundTag":"file-already-exists","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}