我花了3个小时寻找任何样品并经历了许多文章 . 我试图让WCF adhoc发现机制为自托管的Windows服务工作 . 如果我在同一台机器上运行客户端它可以工作,但是在另一台机器上却没有 . 每个教程/样本(方便地)都在同一台工作的机器上显示它 .

  • 我在两台机器上禁用了防火墙 .

  • 如果我直接在客户端使用终点,它可以工作 . 因此,它只是服务发现无法正常工作 .

这是我的服务器代码:

static void Main(string[] args)
    {
        Uri baseAddress = new Uri(string.Format("http://{0}:12345/discovery/Myservice/", System.Net.Dns.GetHostName()));
        Console.WriteLine(baseAddress);
        using (ServiceHost serviceHost = new ServiceHost(typeof(SampleService.Service1), baseAddress))
        {
            serviceHost.AddServiceEndpoint(typeof(SampleService.IService1), new BasicHttpBinding(), string.Empty);
            serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

            ServiceMetadataBehavior metaDataBehavior = new ServiceMetadataBehavior();
            metaDataBehavior.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(metaDataBehavior);

            serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
            serviceHost.Open();
            Console.WriteLine("Press to terminate service.");
            Console.ReadLine();
        }
    }

这是我的客户端代码:

static void InvokeService()
    {
        Console.WriteLine("\nFinding Service ..");
        DiscoveryClientBindingElement discoveryClientBindingElement =
              new DiscoveryClientBindingElement();
        var Services =
             discoveryClientBindingElement.FindCriteria = new FindCriteria(typeof(ServiceReference1.IService1));

        discoveryClientBindingElement.DiscoveryEndpointProvider = new UdpDiscoveryEndpointProvider();

        // The service uses the BasicHttpBinding, so use that and insert the DiscoveryClientBindingElement at the 
        // top of the stack
        CustomBinding binding = new CustomBinding(new BasicHttpBinding());
        binding.Elements.Insert(0, discoveryClientBindingElement);

        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(binding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
        Console.WriteLine("Data = " + client.GetData(1));
    }

任何帮助是极大的赞赏 .