0

我知道WCF service如何在一台机器上simple comsole application client运行以及谁在另一台机器上运行。服务器做了一些非常简单的事情:一种返回客户端发送的数字 2 的值的方法:

[ServiceContract]
public interface IMySampleWCFService
{
    [OperationContract]
    int Add(int num1, int num2);

    [OperationContract]
    void CreateDirectory(string directory);

    [OperationContract]
    string GetVendorToRun(string path);
}

    public class MySampleWCFService : IMySampleWCFService
    {
        public int Add(int num1, int num2)
        {
            return num1 + num2;
        }
    }

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WCFServiceHostingInWinService.MySampleWCFService">
        <endpoint
          name="ServiceHttpEndPoint"
          address="" 
          binding="basicHttpBinding"
          contract="WCFServiceHostingInWinService.IMySampleWCFService">
        </endpoint>
        <endpoint
          name="ServiceMexEndPoint"
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

我想要做并且发现很难实现,因为我是一个新开发人员,将 Discovery 添加到我的WCF service,假设我在几台机器上安装了几个服务,客户端应用程序是打开的我现在想要哪些服务是活动的\正在运行以及我可以从 Discovery 收到的所有这些数据。我尝试阅读几篇文章,但正如我提到的那样,我不明白如何去做,我希望得到一些帮助。

4

2 回答 2

2

使用WCF Discovery有点复杂,根据我的经验,很少有人真正使用它,但它确实有效。这篇MSDN 文章包含将发现添加到服务和客户端配置文件所需的所有详细信息。

WCF 发现背后的前提是您以与默认 MEX 终结点类似的方式公开新发现终结点。MEX 端点允许服务向客户端提供 WSDL。WCF 发现终结点通过对基于客户端 UDP 的请求的基于 UDP 的响应向客户端公开配置的服务。上面的概述链接提供了更多详细信息。

您的服务配置如下所示:

<system.serviceModel>
    <services>
      <service name="WCFServiceHostingInWinService.MySampleWCFService">
        <endpoint
          name="ServiceHttpEndPoint"
          address="" 
          binding="basicHttpBinding"
          contract="WCFServiceHostingInWinService.IMySampleWCFService"
          behaviorConfiguration="endpointDiscoveryBehavior">
        </endpoint>
        <endpoint
          name="ServiceMexEndPoint"
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
        <!-- Discovery Endpoint: -->
        <endpoint kind="udpDiscoveryEndpoint" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
          </baseAddresses>
        </host>
      </service>
      <!-- Announcement Listener Configuration -->
      <service name="AnnouncementListener">
        <endpoint kind="udpAnnouncementEndpoint" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceDiscovery>
            <announcementEndpoints>
              <endpoint kind="udpAnnouncementEndpoint"/>
            </announcementEndpoints>
          </serviceDiscovery>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="endpointDiscoveryBehavior">
          <endpointDiscovery enabled="true"/>
        </behavior>
     </endpointBehaviors>
    </behaviors>
</system.serviceModel>
于 2013-10-23T13:17:10.913 回答
0

我认为最简单的方法是尝试将您的客户端连接到您在运行时计算的地址。例如:

    static void Main(string[] args)
    {
        var addresses = new List<string>
        {
            @"http://192.168.1.1:8730/MySampleWCFService/",
            @"http://localhost:8731/MySampleWCFService/",
            @"http://localhost:8732/MySampleWCFService/",
            @"http://localhost:8733/MySampleWCFService/",
        };
        foreach (var address in addresses)
        {
            var client = new MySampleWCFServiceClient(new BasicHttpBinding(), new EndpointAddress(address));
            try
            {
                client.Open();
                client.Add(0, 1);
                Console.WriteLine("Connected to {0}", address);

            }
            catch (EndpointNotFoundException)
            {
                Console.WriteLine("Service at {0} is unreachable", address);
            }
        }
        Console.ReadLine();
    }

在我的情况下,我创建了一个包含地址的列表,但在您的情况下,您可以使用一些预定义的规则来构建地址。例如,您知道服务使用带有某些名称和端口的 http 绑定。您还知道您的集群位于 192.0.16.xxx LAN 中,因此您可以使用公式:

地址 = “http://” + NextLanAddress() + “:” + 端口 + “/” + 服务名称 + “/”;

于 2013-10-23T13:13:51.983 回答