简体   繁体   English

将Discovery添加到我的WCF服务

[英]Add Discovery to my WCF service

I have WCF service how run on 1 machine and simple comsole application client who run on another machine. 我有WCF service如何在一台机器上运行,以及在另一台机器上运行的simple comsole application client机。 the server do something very simple: one method that return the value of 2 number that the client sent: 服务器做一些非常简单的事情:一个返回客户端发送的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;
        }
    }

App.config: App.config中:

<?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>

What i want to do and find it hard to implement because i am a new developer is add Discovery to my WCF service , suppose i have several services that installed on several machines, what the client application is open i want to now which services is alive\\running and all this data that i can received from Discovery. 我想做什么,发现很难实现,因为我是一个新的开发人员是将Discovery添加到我的WCF service ,假设我有几个服务安装在几台机器上,客户端应用程序打开了什么我想现在哪些服务还活着\\运行和我可以从Discovery收到的所有这些数据。 I try to read several articles but as i mention didn't understand how to do it and I would love some help. 我尝试阅读几篇文章,但正如我提到的那样,我不明白该怎么做,我会喜欢一些帮助。

Using WCF Discovery is a bit convoluted and few people actually use it in my experience but it does work. 使用WCF发现有点令人费解,很少有人在我的经验中实际使用它,但它确实有效。 This MSDN article has all the detail needed for adding Discovery to both the service & client configuration files. MSDN文章包含将Discovery添加到服务和客户端配置文件所需的所有详细信息。

The premise behind WCF Discovery is that you a expose a new discovery endpoint in similar way to the default MEX endpoint. WCF发现背后的前提是您以与默认MEX端点类似的方式公开新的发现端点。 The MEX endpoint allows the service to provide WSDL to clients. MEX端点允许服务向客户端提供WSDL。 A WCF Discovery endpoint exposes a configured service to clients by means of UDP based responses to client UDP based requests. WCF发现端点通过基于UDP的客户端UDP响应响应向客户端公开已配置的服务。 The overview link above provides much more detail. 上面的概述链接提供了更多细节。

Here is how your service configuration would look like: 以下是您的服务配置的外观:

<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>

I think the most easy way to do this is to try to connect your client to addresses that you calculate at the runtime. 我认为最简单的方法是尝试将客户端连接到运行时计算的地址。 For example: 例如:

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

In my case I create a list with addresses but in your case you may build addresses with some predefined rules. 在我的情况下,我创建一个包含地址的列表但在您的情况下,您可以使用一些预定义的规则构建地址 For example you know that services use http binding with some name and port. 例如,您知道服务使用具有某些名称和端口的http绑定。 Also you know that your cluster is in 192.0.16.xxx LAN so you may use formula: 您也知道您的群集位于192.0.16.xxx LAN中,因此您可以使用公式:

address = "http://" + NextLanAddress() + ":" + port + "/" + serviceName + "/"; address =“http://”+ NextLanAddress()+“:”+ port +“/”+ serviceName +“/”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM