用“ ServiceDiscovery.Dnssd”做广告,用Bonjour发现吗?

时间:2018-07-11 13:55:06

标签: c# uwp windows-10 bonjour dns-sd

Windows.Networking.ServiceDiscovery.Dnssd namespace是否允许宣传可通过Apple的Bonjour发现的服务?

Microsoft的文档中提到了所有正确的DNS-SD RFC,但是我无法使用此Bonjour Browser program找到我的程序。我知道Bonjour浏览器程序正在运行,因为它发现了我网络上的其他设备以及通过mDNSResponder通告的本地服务。微软关于此命名空间的文档含糊不清,让我感到奇怪。

此代码中明显有什么不对吗?

async Task RegisterServiceAsync()
{
    var hostNames = NetworkInformation
        .GetHostNames()
        .Where(x => x.Type == HostNameType.DomainName)
        .Select(x => new KeyValuePair<string, HostName>(x.ToString(), x))
        .ToList();
    var hostName = Pick("Pick a host name:", hostNames);
    Console.WriteLine($"Host name is \"{hostName}\"");

    var service = new DnssdServiceInstance(
        dnssdServiceInstanceName: "abc._tcp.local.",
        hostName: hostName,
        port: 13337
    );
    using (var socket = new StreamSocketListener())
    {
        var registration = await service.RegisterStreamSocketListenerAsync(socket);
        Console.WriteLine(registration.Status);
        Console.WriteLine($"Renamed: {registration.HasInstanceNameChanged}");
        Console.WriteLine($"Service instance name: {service.DnssdServiceInstanceName}");
        Console.ReadKey(true);
    }
}

示例输出:

        1) hostname.domain.local
        2) hostname.local
Pick a host name: (1-2) 2
Host name is "hostname.local"
Success
Renamed: False
Service instance name: abc._tcp.local.

1 个答案:

答案 0 :(得分:0)

dnssdServiceInstanceName的格式错误。它必须是<<instance name>>._<<service name>>._<<protocol>>.<<domain>>.。例如,此代码有效:

async Task RegisterServiceAsync()
{
    var hostNames = NetworkInformation
        .GetHostNames()
        .Where(x => x.Type == HostNameType.DomainName)
        .Select(x => new KeyValuePair<string, HostName>(x.ToString(), x))
        .ToList();
    var hostName = Pick("Pick a host name:", hostNames);
    Console.WriteLine($"Host name is \"{hostName}\"");

    var service = new DnssdServiceInstance(
        dnssdServiceInstanceName: "instanceName._abcservice._tcp.local.",
        hostName: hostName,
        port: 13337
    );
    using (var socket = new StreamSocketListener())
    {
        var registration = await service.RegisterStreamSocketListenerAsync(socket);
        Console.WriteLine(registration.Status);
        Console.WriteLine($"Renamed: {registration.HasInstanceNameChanged}");
        Console.WriteLine($"Service instance name: {service.DnssdServiceInstanceName}");
        Console.ReadKey(true);
    }
}

我使用Microsoft示例程序的this line弄清了这一点。