【问题标题】:DiscoveryClient is not working on other machines than mineDiscoveryClient 不在我的其他机器上工作
【发布时间】:2019-12-03 12:13:20
【问题描述】:

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

服务器:

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)
     {
         ....
     }

发现服务:

DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var services = discoveryClient.Find(new FindCriteria(typeof(IUpdateServerService)));
discoveryClient.Close();

在本地机器上这工作正常。找到服务。另一方面,当有人在其他机器上运行客户端时,似乎找不到服务(客户端应该在服务器上注册自己,但它没有)。

有什么问题吗?我渴望的想法是 UDP 流量可以被防火墙阻止。我怎样才能改变它不使用UDP?所有客户端和服务器都在同一个域中。

【问题讨论】:

    标签: wcf service-discovery


    【解决方案1】:

    此类问题主要是由于防火墙问题,我们必须添加防火墙规则以允许所有 UDP 流量。
    Allowing WCF udpDiscoveryEndpoint though firewall
    WCF Discovery simply doesn't work
    微软文档。
    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);
    }
    

    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多