DiscoveryClient不在我的其他机器上工作

时间:2019-12-03 12:13:20

标签: wcf service-discovery

我有使用服务发现的客户端/服务器应用程序:

服务器:

scenario("check")
.feed(ids)
.exec(http("check")
  .post("/check")
  .header("X-Token", session => session("token").as[String])
  .body(StringBody(session => 
    """
       |{
       |  "id": ${id},
       |  "subId": ${subId}
       |  "addressId": """" + session("token").as[String] + """"
       |}
    """.stripMargin
  ))

发现服务:

using (ServiceHost host = new ServiceHost(Instance, baseAddress))
{
    try
    {
         host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
         host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
         ...

         host.Open();
         while (!Program.IsExit && !cancel)
         {
             Thread.Sleep(50);
         }
         host.Close();
     }
     catch (Exception ex)
     {
         ....
     }

在本地计算机上可以正常工作。找到服务。另一方面,似乎找不到在其他计算机服务上运行客户端的客户端(客户端应在服务器上注册自己,但不能注册)。

有什么问题?我的渴求是防火墙可以阻止UDP通信。我该如何更改为不使用UDP?所有客户端和服务器都在同一域中。

1 个答案:

答案 0 :(得分:0)

这种问题主要是由于防火墙问题造成的,我们必须添加防火墙规则以允许所有UDP流量。
Allowing WCF udpDiscoveryEndpoint though firewall
WCF Discovery simply doesn't work
Microsoft文档。
https://web.archive.org/web/20161111140652/https://msdn.microsoft.com/en-us/library/dd352335.aspx
我们还可以使用HTTP协议发现客户端。这是一个例子。
服务器端。

<system.serviceModel>
    <services>
      <service name="ConsoleApp4.Service1">
        <endpoint address=""
                  binding="wsHttpBinding" contract="ConsoleApp4.IService1"/>
        <endpoint kind="discoveryEndpoint" address="http://localhost:1000/dis" binding="wsHttpBinding" bindingConfiguration=""></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://vabqia969vm:22000"/>
          </baseAddresses>
        </host>
      </service>
    </services>
   <bindings>
     <wsHttpBinding>
       <binding>
         <security mode="None"></security>
       </binding>
     </wsHttpBinding>
   </bindings>
   <behaviors>
     <serviceBehaviors>
       <behavior>
         <serviceDiscovery></serviceDiscovery>
       </behavior>
     </serviceBehaviors>
   </behaviors>
  </system.serviceModel>

客户端。 配置。

    <system.serviceModel>
      <client>
        <endpoint name="dis_client" kind="discoveryEndpoint" address="http://vabqia969vm:1000/dis" binding="wsHttpBinding"></endpoint>
      </client>
    <bindings>
      <wsHttpBinding>
        <binding>
          <security mode="None"></security>
        </binding>
      </wsHttpBinding>
    </bindings>
</system.serviceModel>

代码。

DiscoveryClient client = new DiscoveryClient("dis_client");
FindResponse response = client.Find(new FindCriteria(typeof(IService1)));
foreach (EndpointDiscoveryMetadata item in response.Endpoints)
{
    WSHttpBinding binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.None;
    ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, new EndpointAddress(item.Address.Uri));
    var service = factory.CreateChannel();
    var result = service.GetData(34);
    Console.WriteLine(result);
}

请随时告诉我是否有什么可以帮忙的。