{"record":{"id":"43e8359a295d1152","repo":"TechnitiumSoftware/DnsServer","slug":"scope-with-name-newname-already-exists","errorCode":null,"errorMessage":"Scope with name '{newName}' already exists.","messagePattern":"Scope with name '(.+?)' already exists\\.","errorType":"exception","errorClass":"DhcpServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dhcp/DhcpServer.cs","lineNumber":1449,"sourceCode":"        }\n\n        public Scope GetScope(string name)\n        {\n            if (_scopes.TryGetValue(name, out Scope scope))\n                return scope;\n\n            return null;\n        }\n\n        public void RenameScope(string oldName, string newName)\n        {\n            Scope.ValidateScopeName(newName);\n\n            if (!_scopes.TryGetValue(oldName, out Scope scope))\n                throw new DhcpServerException(\"Scope with name '\" + oldName + \"' does not exists.\");\n\n            if (!_scopes.TryAdd(newName, scope))\n                throw new DhcpServerException(\"Scope with name '\" + newName + \"' already exists.\");\n\n            scope.Name = newName;\n            _scopes.TryRemove(oldName, out _);\n\n            SaveScopeFile(scope);\n            DeleteScopeFile(oldName);\n        }\n\n        public void DeleteScope(string name)\n        {\n            if (_scopes.TryGetValue(name, out Scope scope))\n            {\n                UnloadScope(scope, true);\n                DeleteScopeFile(scope.Name);\n            }\n        }\n\n        public async Task<bool> EnableScopeAsync(string name, bool throwException = false)","sourceCodeStart":1431,"sourceCodeEnd":1467,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dhcp/DhcpServer.cs#L1431-L1467","documentation":"RenameScope found the source scope but the target newName is already taken by another scope, so TryAdd(newName, scope) fails. Rename is atomic: it adds the new key before removing the old.","triggerScenarios":"Calling RenameScope(oldName, newName) when a different scope already has Name == newName.","commonSituations":"Renaming onto an existing scope name. UI pre-fill collision. Swapping names without an intermediate.","solutions":["Choose a newName not currently in use (check GetScope(newName) == null first).","Rename or delete the conflicting target scope first.","For a swap, rename through a temporary unique name."],"exampleFix":"// before\n_dhcpServer.RenameScope('a', 'b'); // 'b' exists\n// after\nif (_dhcpServer.GetScope('b') == null) _dhcpServer.RenameScope('a', 'b');","handlingStrategy":"validation","validationCode":"if (_dhcpServer.GetScope(newName) != null)\n    throw new InvalidOperationException($\"Scope name '{newName}' already exists\");","typeGuard":"static bool NewNameFree(Func<string, Scope> getScope, string name) => getScope(name) == null;","tryCatchPattern":"try { _dhcpServer.RenameScope(oldName, newName); }\ncatch (DhcpServerException ex) when (ex.Message.Contains(\"already exists\"))\n{ /* pick a unique name or delete/swap target, retry */ }","preventionTips":["Check GetScope(newName) == null before renaming.","Use a temporary unique name when swapping two scope names.","Avoid duplicate names across imports."],"tags":["dhcp","scope","validation","duplicate"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}