0
Answer

WCF Discovery - Client Discovering Old Service

Michael Laterza

Michael Laterza

14y
4.4k
1

Hello,
I have a WCF service for which I've enabled WS-Discovery.  I changed the binding from net.tcp to basicHttp and after rebuilding and even rebooting the machine, the client is discovering both the new basicHttp service and also the old net.tcp service.  Is there a way to stop the old service from being active (rebooting didn't help).
Here is the code for the host:
int availablePort = GetAvailablePort.GetPort();

      Uri baseAddress = new Uri("http://localhost:" + availablePort + "/MyServer");

      if (selfHost != null)
      {
        selfHost.Close();
        selfHost = null;
      }

      // Create the ServiceHost.
      selfHost = new ServiceHost(typeof(MyService), baseAddress);

      try
      {
        // Configure the Binding.
        var binding = new BasicHttpBinding("BasicHttpBinding_IMyServiceContract");

        // Add a ServiceDiscoveryBehavior.
        selfHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

        // Add a UdpDiscoveryEndpoint.
        selfHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

        // Add a service endpoint.
        selfHost.AddServiceEndpoint(typeof(IMyServiceContract), binding, baseAddress);

        // Start the service.
        selfHost.Open();
        Console.WriteLine("MyServer Started" + "at: " + baseAddress);
        Console.WriteLine("Press <Enter> to terminate the service.");
        Console.WriteLine();
        Console.ReadLine();

        // Close the ServiceHostBase to shutdown the service.
        selfHost.Close();
      }
      catch (CommunicationException ce)
      {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        selfHost.Abort();
      }

 
and here is the client code:
 
private static EndpointAddress FindMyServerServiceAddress()
    {
      // Create DiscoveryClient
      DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

      // Create FindCriteria
      FindCriteria findCriteria = new FindCriteria(typeof(IMyServiceContract));
      findCriteria.MaxResults = 1;

      // Find IMyServiceContract endpoints
      FindResponse findResponse = discoveryClient.Find(findCriteria);

      Console.WriteLine();
      Console.WriteLine("Discovered {0} IMyServiceContract endpoint(s).", findResponse.Endpoints.Count);

      if (findResponse.Endpoints.Count > 0)
      {
        return findResponse.Endpoints[0].Address;
      }
      else
      {
        return null;
      }
    }

 
You can see that the host is now being created with an http address, but the client is discovering the old net.tcp address as well...