【问题标题】:How do I create an SRV record in DNS with C#如何使用 C# 在 DNS 中创建 SRV 记录
【发布时间】:2025-12-27 08:50:11
【问题描述】:

我正在使用 WMI 创建不同类型的 DNS 记录,但 SRV 记录有问题。每当我传递 DomainName 参数时,我都会收到“未找到”错误。我觉得这个域名不错。

有人成功过吗?

这是我的代码:

internal static void CreateSrvRecordInDns(string Zone, string OwnerName, string DomainName, UInt16 Weight, UInt16 Priority, UInt16 Port)
    {
        DnsProvider dns = new DnsProvider();
        ManagementClass mClass = new ManagementClass(dns.Session, new ManagementPath("MicrosoftDNS_SrvType"), null);
        ManagementBaseObject inParams = mClass.GetMethodParameters("CreateInstanceFromPropertyData");
        inParams["DnsServerName"] = dns.Server;
        inParams["ContainerName"] = Zone;
        inParams["OwnerName"] = OwnerName;
        inParams["DomainName"] = DomainName; //Error occurs here
        inParams["Port"] = Port;
        inParams["Priority"] = Priority;
        inParams["Weight"] = Weight;
        mClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
        dns.Dispose(ref inParams);
        dns.Dispose(ref mClass);
    }

【问题讨论】:

  • 你传递的域名是什么?
  • 你的意思是 _finger._tcp.parentdomain 吗? rfc2782
  • 我同意 dtb,您的意思可能是 _finger._tcp.parentdomain,并且由于您将这两个部分倒过来,因此 _finger 子域(或文件夹)不存在,因此你的错误。
  • 我尝试了您的建议,但没有帮助。我遇到了同样的错误。

标签: c# dns wmi srv


【解决方案1】:

正确的 SRV 记录是 _finger._tcp.example.com

我不知道 WMI,但系统可能要求您首先为 _tcp.example.com 创建“空非终端”节点。

编辑

我相信我现在看到了问题 - 您的 OwnerName 字段应该是包含 _finger._tcp.example.com 的字段。 DomainName 字段应该包含SRV 记录的目标

http://msdn.microsoft.com/en-us/library/ms682736%28v=VS.85%29.aspx

【讨论】:

  • 感谢您的建议。我尝试先创建域,但无济于事。当我尝试传递 DomainName 参数时,我仍然遇到同样的错误。
  • 如果不带下划线试试会怎样? WMI 可能错误地认为它们不合法。
  • 再次感谢,但仍然没有运气。我想我是唯一一个尝试过这个的人。
【解决方案2】:

只需将有问题的行替换为:

inParams["SRVDomainName"] = DomainName;

我不知道原因,但是当得到属性列表时:

PropertyData[] pd = new PropertyData[inParams.Properties.Count];
inParams.Properties.CopyTo(pd,0);

这是该字段的名称(微软的错误?)

HTH。

附:为了查看每个字段的正确格式,请使用 wbemtest 工具(来自命令提示符的 wbemtest),连接到 root\MicrosoftDNS 命名空间并运行以下查询:

Select * from MicrosoftDNS_SRVType

您应该使用与答案中列出的实例相同的格式。

【讨论】:

  • 谢谢!对于解决方案和 wbemtest 工具引起我的注意。
【解决方案3】:

我想在这里为那些仍然无法获得它的人添加一些细节......

如果您的域名google.com并且如果记录是:_finger._tcp.google.com 指向 target host : hello.google.com 然后变量及其值如下:

    inParams["DnsServerName"] = dns.Server;
    inParams["ContainerName"] = Zone; //google.com
    inParams["OwnerName"] = OwnerName; //_finger._tcp.google.com
    // Can't set domain name like this, leave this field
    //inParams["DomainName"] = DomainName; //_tcp.google.com
    //Set Target SRV Host here which is providing the service,,,
    inParams["SRVDomainName"] = DomainName; //target Host : hello.google.com

    inParams["Port"] = Port;
    inParams["Priority"] = Priority;
    inParams["Weight"] = Weight;

我已经通过创建示例应用程序和创建区域 google.com 并设置 SRV 记录及其值进行了测试,如上所述。我希望它对那些其他回复可能听起来不太解释的人有所帮助。

【讨论】:

  • 更正了一个错误。尝试创建 SRV 记录时设置域名不正确!要设置 TargetHost,必须设置 SRVDomainName 属性,遗憾的是,该属性在任何地方都没有记录... :(