{"record":{"id":"ccf3a0f0b30d58d1","repo":"TechnitiumSoftware/DnsServer","slug":"zone-was-not-found-for-domain-key","errorCode":null,"errorMessage":"Zone was not found for domain: {key}","messagePattern":"Zone was not found for domain: (.+?)","errorType":"exception","errorClass":"DnsServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Trees/AuthZoneTree.cs","lineNumber":639,"sourceCode":"                                currentKey = value.Key;\n                            }\n                        }\n                        while (true);\n                    }\n                }\n            }\n\n            return zones;\n        }\n\n        public AuthZone GetOrAddSubDomainZone(string zoneName, string domain, Func<SubDomainZone> valueFactory)\n        {\n            bool isApex = zoneName.Equals(domain, StringComparison.OrdinalIgnoreCase);\n\n            AuthZoneNode authZoneNode = GetOrAdd(domain, delegate (string key)\n            {\n                if (isApex)\n                    throw new DnsServerException(\"Zone was not found for domain: \" + key);\n\n                return new AuthZoneNode(valueFactory(), null);\n            });\n\n            if (isApex)\n            {\n                if (authZoneNode.ApexZone is null)\n                    throw new DnsServerException(\"Zone was not found: \" + zoneName);\n\n                return authZoneNode.ApexZone;\n            }\n            else\n            {\n                return authZoneNode.GetOrAddParentSideZone(valueFactory);\n            }\n        }\n\n        public AuthZone GetAuthZone(string zoneName, string domain)","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Trees/AuthZoneTree.cs#L621-L657","documentation":"Thrown by AuthZoneTree.GetOrAddSubDomainZone when the requested domain equals the zone apex (zoneName == domain, case-insensitive) but no AuthZoneNode for that domain exists in the tree, so the GetOrAdd value-factory delegate runs and refuses to fabricate an apex node. It signals that the caller asked for the apex zone itself while only a sub-domain zone can be created on demand. The tree intentionally will not auto-create an apex; it must be registered beforehand.","triggerScenarios":"Calling GetOrAddSubDomainZone(zoneName, domain) with domain equal to zoneName (apex) before the apex zone has been added to the AuthZoneTree via TryAdd/apex registration; the GetOrAdd factory delegate is invoked for the missing domain node and immediately throws because isApex is true.","commonSituations":"Adding records to a zone whose apex was never created (CreateEmptyApexZone not yet called), misordered zone initialization that creates sub-domains before the apex is registered, or a race where the apex was deleted concurrently with a sub-domain add request.","solutions":["Ensure the apex zone is created and registered in the tree (via AuthZoneManager.CreateEmptyApexZone / _root.TryAdd) BEFORE calling GetOrAddSubDomainZone for any apex-equal domain.","If the apex may legitimately be absent, check with TryGet(zoneName) first and only call GetOrAddSubDomainZone when zoneName != domain.","Catch DnsServerException and surface a clearer 'apex zone must be created first' error to the upstream caller."],"exampleFix":"// before\nvar sub = _root.GetOrAddSubDomainZone(zoneName, zoneName, factory);\n\n// after\nif (!_root.TryGet(zoneName, out _))\n    throw new DnsServerException(\"Apex zone must be created before adding sub-domains: \" + zoneName);\nvar sub = _root.GetOrAddSubDomainZone(zoneName, childDomain, factory);","handlingStrategy":"validation","validationCode":"// Ensure apex is registered and domain != zoneName before calling sub-domain ops\nbool CanGetOrAddSubDomain(AuthZoneTree root, string zoneName, string domain)\n{\n    if (zoneName.Equals(domain, StringComparison.OrdinalIgnoreCase))\n        return root.TryGet(domain, out var node) && node.ApexZone is not null;\n    return root.TryGet(zoneName, out _);\n}","typeGuard":null,"tryCatchPattern":"try { return root.GetOrAddSubDomainZone(zoneName, domain, factory); }\ncatch (DnsServerException ex) when (ex.Message.StartsWith(\"Zone was not found for domain\"))\n{\n    // apex missing — create/retrieve it, then retry or surface upstream\n    throw new InvalidOperationException(\"Apex zone not initialized: \" + zoneName, ex);\n}","preventionTips":["Always create and register the apex zone before any sub-domain operation.","Guard sub-domain calls with a TryGet(zoneName) check.","Treat apex-equal domain arguments to GetOrAddSubDomainZone as a caller bug."],"tags":["dns","csharp","zone-management","api-misuse","technitium"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}