{"record":{"id":"2de81941cb2b6b1d","repo":"TechnitiumSoftware/DnsServer","slug":"scope-with-same-name-already-exists","errorCode":null,"errorMessage":"Scope with same name already exists.","messagePattern":"Scope with same name already exists\\.","errorType":"exception","errorClass":"DhcpServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dhcp/DhcpServer.cs","lineNumber":1235,"sourceCode":"                if (throwException)\n                    throw;\n            }\n\n            return false;\n        }\n\n        private async Task LoadScopeAsync(Scope scope, bool waitForInterface)\n        {\n            foreach (KeyValuePair<string, Scope> entry in _scopes)\n            {\n                Scope existingScope = entry.Value;\n\n                if (existingScope.IsAddressInRange(scope.StartingAddress) || existingScope.IsAddressInRange(scope.EndingAddress))\n                    throw new DhcpServerException(\"Scope with overlapping range already exists: \" + existingScope.StartingAddress.ToString() + \"-\" + existingScope.EndingAddress.ToString());\n            }\n\n            if (!_scopes.TryAdd(scope.Name, scope))\n                throw new DhcpServerException(\"Scope with same name already exists.\");\n\n            if (scope.Enabled)\n            {\n                if (!await ActivateScopeAsync(scope, waitForInterface))\n                    scope.SetEnabled(false);\n            }\n\n            _log.Write(\"DHCP Server successfully loaded scope: \" + scope.Name);\n        }\n\n        private void UnloadScope(Scope scope, bool removeDnsEntries)\n        {\n            if (scope.Enabled)\n                DeactivateScope(scope, removeDnsEntries, false);\n\n            if (_scopes.TryRemove(scope.Name, out Scope removedScope))\n            {\n                removedScope.Dispose();","sourceCodeStart":1217,"sourceCodeEnd":1253,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dhcp/DhcpServer.cs#L1217-L1253","documentation":"The new scope's Name already exists in the concurrent scopes dictionary, so TryAdd fails. Names are the unique key for scopes.","triggerScenarios":"Loading/adding a scope whose Name equals an already-loaded scope's Name; `_scopes.TryAdd(scope.Name, scope)` returns false.","commonSituations":"Duplicate scope file in the scopes directory. Renaming then re-adding without deleting the old. Race where two scope files share a name.","solutions":["Pick a unique scope Name before adding.","Delete the existing scope with the same name first, or rename it.","Remove duplicate scope files from the scopes directory."],"exampleFix":"// before\n_dhcpServer.AddScope(new Scope('lan', ...)); // 'lan' already exists\n// after\n_dhcpServer.DeleteScope('lan');\n_dhcpServer.AddScope(new Scope('lan', ...));","handlingStrategy":"validation","validationCode":"if (_dhcpServer.GetScope(scope.Name) != null)\n    throw new InvalidOperationException($\"Scope name '{scope.Name}' already exists\");","typeGuard":"static bool ScopeNameFree(Func<string, Scope> getScope, string name) => getScope(name) == null;","tryCatchPattern":"try { await _dhcpServer.LoadScopeAsync(scope, true); }\ncatch (DhcpServerException ex) when (ex.Message.Contains(\"same name already exists\"))\n{ /* rename or delete existing scope, retry */ }","preventionTips":["Generate unique scope names (e.g. site-subnet tag).","Delete superseded scope files before re-importing.","Check GetScope before AddScope in automation."],"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"}