2013-10-23 73 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