2013-10-23 29 views
0

我有WCF service如何在一台机器上运行,simple comsole application client谁在另一台机器上运行。 服务器做一些很简单:返回客户端发送的2号的值一个方法:添加发现到我的WCF服务

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

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

什么我想要做的,很难找到它实施,因为我是一个新的开发人员是添加发现我的WCF service,假设我有几个服务器安装在几台机器上,什么客户端应用程序打开我想现在哪些服务是活着\运行和所有这些数据,我可以从发现。 我尝试阅读几篇文章,但正如我提到不明白如何去做,我会喜欢一些帮助。

回答

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中,因此您可以使用公式:

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

+0

[WCF发现(http://msdn.microsoft.com/en-us/library/dd456791.aspx)。这个想法是,你*提前不知道*可能的地址。 – shambulator

2

使用WCF Discovery有点复杂,实际上很少有人使用它,但它确实有效。此MSDN article具有将发现添加到服务&客户端配置文件所需的所有详细信息。

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

它看起来如此复杂...我甚至很难将它添加到app.config – user2214609

+1

是的,我猜“有点复杂”是轻描淡写...... ;-) –

+0

我可以从代码而不是从配置添加发现档案? – user2214609