TechnitiumSoftware/DnsServer · error · InvalidOperationException

Cannot add record: {type} record already exists for the same

Error message

Cannot add record: {type} record already exists for the same name.

What it means

AuthZone.SetRecords (public virtual) enforces exclusivity for CNAME, DNAME, and APP record types. If the _entries dictionary is non-empty and does NOT already contain the requested type, it throws InvalidOperationException indicating that the requested type 'already exists' (meaning other incompatible types exist and the requested exclusive type cannot be added). CNAME and DNAME are exclusive per RFC 1034; APP is a Technitium-specific application record that is also treated as exclusive.

Source

Thrown at DnsServerCore/Dns/Zones/AuthZone.cs:817

                foreach (DnsResourceRecord nsRecord in nsRecords)
                    nsRecord.SyncGlueRecords(deletedGlueRecords, addedGlueRecords);
            }
        }

        public void LoadRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
        {
            _entries[type] = records;
        }

        public virtual void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
        {
            switch (type)
            {
                case DnsResourceRecordType.CNAME:
                case DnsResourceRecordType.DNAME:
                case DnsResourceRecordType.APP:
                    if ((!_entries.IsEmpty) && !_entries.ContainsKey(type))
                        throw new InvalidOperationException($"Cannot add record: {type} record already exists for the same name.");

                    break;

                case DnsResourceRecordType.NSEC:
                case DnsResourceRecordType.RRSIG:
                    break; //ignore

                default:
                    if (_entries.ContainsKey(DnsResourceRecordType.CNAME))
                        throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");

                    break;
            }

            _entries[type] = records;
        }

        public virtual bool AddRecord(DnsResourceRecord record)

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Clear all conflicting record types from the zone node before calling SetRecords for CNAME/DNAME/APP.
  2. If updating an existing CNAME/DNAME/APP, the same type already present is allowed (the check only fires when the type is absent but other types exist).
  3. Use a separate name for the exclusive-type record to avoid the conflict.

Example fix

// before
zone.SetRecords(DnsResourceRecordType.CNAME, cnameList);
// throws if A/AAAA/MX records exist at this name

// after
foreach (var t in existingTypes.Where(t => t != DnsResourceRecordType.CNAME))
    zone.DeleteRecords(t);
zone.SetRecords(DnsResourceRecordType.CNAME, cnameList);
Defensive patterns

Strategy: validation

Validate before calling

// For exclusive types (CNAME, DNAME, APP), clear other types first
var exclusiveTypes = new[] { DnsResourceRecordType.CNAME, DnsResourceRecordType.DNAME, DnsResourceRecordType.APP };
if (exclusiveTypes.Contains(type))
{
    foreach (var existingType in zone.GetRecordTypes())
        if (existingType != type)
            zone.DeleteRecords(existingType);
}
zone.SetRecords(type, records);

Type guard

static bool IsExclusiveRecordType(DnsResourceRecordType type)
    => type == DnsResourceRecordType.CNAME
    || type == DnsResourceRecordType.DNAME
    || type == DnsResourceRecordType.APP;

Prevention

When it happens

Trigger: Calling SetRecords with type CNAME, DNAME, or APP when the zone node's _entries already contains records of a different type. For example, calling SetRecords(CNAME, ...) when A records already exist, or SetRecords(DNAME, ...) when other types are present.

Common situations: Converting an existing A/AAAA record to a CNAME alias via SetRecords without first clearing conflicting records; setting an APP record on a name that already has standard DNS records; zone import logic that doesn't account for exclusive-type rules.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/c091b9e3b784c320. Report an issue: GitHub.