1

I am using the Windows.Networking.ServiceDiscovery.Dnssd API in a UWP project to register a DnssdServiceInstance to broadcast around the network using the following code:

        //Service Broadcast
        _listener = new StreamSocketListener();
        _listener.ConnectionReceived += _listener_ConnectionReceived;

        await _listener.BindServiceNameAsync("56788");

        string userid = SettingsHelper.GetSetting("userid").ToString();

        _service = new DnssdServiceInstance(userid + "._listen._tcp.local",
           NetworkInformation.GetHostNames().FirstOrDefault(x => x.Type == HostNameType.DomainName && x.RawName.Contains("local")),
           UInt16.Parse(_listener.Information.LocalPort));

        await _service.RegisterStreamSocketListenerAsync(_listener);

The service is created just fine but is never removed from the network. I keep seeing all created instances with my DeviceWatcher. Even when disposing and setting the StreamSocketListener to null.

Update: When the computer that created the service is restarted, all the created services are gone.. but not when the application that created the service has shut down

There is no mention in the API of service removal. Is this a bug or did I miss something?

Thanks

WJM
  • 1,137
  • 1
  • 15
  • 30

1 Answers1

1

The solution is to both dispose the streamsocket and set the StreamSocketListener and the service instance to null.

From the Microsoft Quiz Game Sample:

    /// <summary>
    /// Unregisters and removes the DNS-SD service. If this method is not called, 
    /// the registration will remain discoverable, even if the app is not running.
    /// </summary>
    public override bool StopAdvertising()
    {
        if (_socket != null && _service != null)
        {
            _socket.ConnectionReceived -= MessageToConnectReceivedFromParticipantAsync;
            _socket.Dispose();
            _socket = null;
            _service = null;

            return true;
        }

        return false;
    }
WJM
  • 1,137
  • 1
  • 15
  • 30