【问题标题】:Search for WCF service in a LAN?在局域网中搜索 WCF 服务?
【发布时间】:2009-03-09 14:26:02
【问题描述】:

有没有办法在本地网络的任何计算机中搜索给定 WCF 服务的存在?

例如,我正在寻找 Math/Add2Numbers 服务,我想知道 LAN 上的哪些机器提供它,有什么办法吗?

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    这是一个超级简单的发现示例。它不使用配置文件,都是 c# 代码,但您可以将概念移植到配置文件中。

    在主机和客户端程序之间共享此接口(暂时复制到每个程序)

    [ServiceContract]
    public interface IWcfPingTest
    {
      [OperationContract]
      string Ping();
    }
    

    将此代码放入宿主程序中

    public class WcfPingTest : IWcfPingTest
    {
      public const string magicString = "djeut73bch58sb4"; // this is random, just to see if you get the right result
      public string Ping() {return magicString;}
    }
    public void WcfTestHost_Open()
    {
      string hostname = System.Environment.MachineName;
      var baseAddress = new UriBuilder("http", hostname, 7400, "WcfPing");
      var h = new ServiceHost(typeof(WcfPingTest), baseAddress.Uri);
    
      // enable processing of discovery messages.  use UdpDiscoveryEndpoint to enable listening. use EndpointDiscoveryBehavior for fine control.
      h.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
      h.AddServiceEndpoint(new UdpDiscoveryEndpoint());
    
      // enable wsdl, so you can use the service from WcfStorm, or other tools.
      var smb = new ServiceMetadataBehavior();
      smb.HttpGetEnabled = true;
      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
      h.Description.Behaviors.Add(smb);
    
      // create endpoint
      var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
      h.AddServiceEndpoint(typeof(IWcfPingTest) , binding,   "");
      h.Open();
      Console.WriteLine("host open");
    }
    

    将此代码放入客户端程序中

    private IWcfPingTest channel;
    public Uri WcfTestClient_DiscoverChannel()
    {
      var dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
      FindCriteria fc = new FindCriteria(typeof(IWcfPingTest));
      fc.Duration = TimeSpan.FromSeconds(5);
      FindResponse fr = dc.Find(fc);
      foreach(EndpointDiscoveryMetadata edm in fr.Endpoints) 
      {
        Console.WriteLine("uri found = " + edm.Address.Uri.ToString());
      }
      // here is the really nasty part
      // i am just returning the first channel, but it may not work.
      // you have to do some logic to decide which uri to use from the discovered uris
      // for example, you may discover "127.0.0.1", but that one is obviously useless.
      // also, catch exceptions when no endpoints are found and try again.
      return fr.Endpoints[0].Address.Uri;  
    }
    public void WcfTestClient_SetupChannel()
    {
      var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
      var factory = new ChannelFactory<IWcfPingTest>(binding);
      var uri = WcfTestClient_DiscoverChannel();
      Console.WriteLine("creating channel to " + uri.ToString());
      EndpointAddress ea = new EndpointAddress(uri);
      channel = factory.CreateChannel(ea);
      Console.WriteLine("channel created");
      //Console.WriteLine("pinging host");
      //string result = channel.Ping();
      //Console.WriteLine("ping result = " + result);
    }
    public void WcfTestClient_Ping()
    {
      Console.WriteLine("pinging host");
      string result = channel.Ping();
      Console.WriteLine("ping result = " + result);
    }
    

    在主机上,只需调用 WcfTestHost_Open() 函数,然后永远休眠之类的。

    在客户端上,运行这些函数。主机打开需要一点时间,所以这里有几个延迟。

    System.Threading.Thread.Sleep(8000);
    this.server.WcfTestClient_SetupChannel();
    System.Threading.Thread.Sleep(2000);
    this.server.WcfTestClient_Ping();
    

    主机输出应该是这样的

    host open
    

    客户端输出应该是这样的

    uri found = http://wilkesvmdev:7400/WcfPing
    creating channel to http://wilkesvmdev:7400/WcfPing
    channel created
    pinging host
    ping result = djeut73bch58sb4
    

    这确实是我能想到的发现示例的最低要求。这些东西很快就会变得相当复杂。

    【讨论】:

      【解决方案2】:

      您需要的是 WS-Discovery,但不幸的是,它不包含在 WCF 中的 ws-* 扩展中。它有一些本土的实现。这是一个google search

      否则,您可以通过第三方供应商(如 IBMMicrosoft)实施企业 UDDI 或注册解决方案。

      【讨论】:

        【解决方案3】:

        您可以使用 UDDI 来查找它,但如果有多个服务实例,您将如何决定使用哪个实例?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多