1

サンプルを探したり、多くの記事を読んだりするのに 3 時間を費やしました。自己ホスト型 Windows サービスで WCF アドホック検出メカニズムを機能させようとしています。同じマシンでクライアントを実行すると動作しますが、別のマシンでは動作しません。すべてのチュートリアル/サンプルは (便利なことに) 動作する同じマシンでそれを示しています。

  • 両方のマシンでファイアウォールを無効にしました。
  • クライアントでエンドポイントを直接使用すると、機能します。したがって、機能していないのはサービス ディスカバリだけです。

ここに私のサーバーコードがあります:

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));
    }

どんな助けでも大歓迎です。

4

0 に答える 0