How do I create an SRV record in DNS with C#
Asked Answered
R

3

8

I am using WMI to create different types of DNS records but am having a problem with SRV records. I keep getting a "Not found" error whenever I pass the DomainName parameter. The domain name looks good to me.

Has anyone ever successfully done this?

Here is my code:

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);
    }
Reincarnate answered 22/9, 2010 at 18:52 Comment(4)
What is the domain name you're passing?Amasa
Do you mean _finger._tcp.parentdomain? rfc2782Wolfram
I would agree with dtb, chances are you're meaning _finger._tcp.parentdomain, and since you're got those two parts backwards, the _finger subdomain (or folder) doesn't exist, hence your error.Amasa
I tried your suggestion but that didn't help. I got the same error.Reincarnate
C
4

Simply replace the problematic line with:

inParams["SRVDomainName"] = DomainName;

I don't know the reason, but when got the properties list by:

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

This was the name of this field (Microsoft's bug?)

HTH.

P.S. In order to see the right format for each field, use wbemtest tool (wbemtest from command prompt), connect to root\MicrosoftDNS namespace and run the following query:

Select * from MicrosoftDNS_SRVType

You should use the same format as the instances listed in the answer).

Checkrow answered 5/10, 2010 at 14:10 Comment(1)
THANK YOU! For both the solution and bringing wbemtest tool to my attention.Reincarnate
S
2

I would like to add a bit of detail here for those who are still unable to get it...

If your domain Name is google.com and if the Record is : _finger._tcp.google.com pointing towards target host : hello.google.com then the variables and their values will be as under:

    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;

I have tested in by creating a sample application and creating zone google.com and setting an SRV record and its values as mentioned up. I hope it helps to those to whom other replies may sound it bit less explanatory.

Sponson answered 6/5, 2011 at 7:43 Comment(1)
Corrected a mistake. Setting DomainName while trying to create an SRV record is incorrect! To Set TargetHost one has to set SRVDomainName property which is sadly not documented anywhere... :(Sponson
W
0

The correct SRV record would be _finger._tcp.example.com.

I don't know WMI, but the system may be requiring you to create the "empty non-terminal" node for _tcp.example.com first.

EDIT

I believe I see the problem now - your OwnerName field should be the one to contain _finger._tcp.example.com. The DomainName field is supposed to contain the target of the SRV record.

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

Waxman answered 23/9, 2010 at 7:12 Comment(3)
Thanks for your suggestions. I've tried creating the domain first but to no avail. I'm still getting the same error when I try to pass the DomainName parameter in.Reincarnate
What happens if you try it without the underscores? It's possible that WMI incorrectly believes that they're not legal.Waxman
Thanks again but still no luck. I think I am the only one to have ever attempted this.Reincarnate

© 2022 - 2024 — McMap. All rights reserved.