2011-10-07 102 views
1

我有一個使用netTcpBinding連接到WCF服務的客戶端。從app.config獲取WCF客戶端端點IP

要連接到我使用我的客戶下列服務:

namespace Embedded_DCC_Client 
{ 
    public class EmbeddedClient 
    { 
     private ChannelFactory<IEmbeddedService> channelFactory; 

     //Embedded DCC TCP Addresses 
     public const String LOCAL_ADDRESS = "net.tcp://localhost:9292/EmbeddedService"; 
     public const String REMOTE_ADDRESS = "net.tcp://192.168.10.42:9292/EmbeddedService"; 

     public IEmbeddedService Proxy { get; private set; } 

     public EmbeddedClient() 
     { 
      //Configure binding 
      NetTcpBinding binding = new NetTcpBinding(); 
      binding.OpenTimeout = TimeSpan.MaxValue; //infinite open timeout 
      binding.CloseTimeout = TimeSpan.MaxValue; //infinite close timeout 
      binding.SendTimeout = TimeSpan.MaxValue; //infinite send timeout 
      binding.ReceiveTimeout = TimeSpan.MaxValue; //infinite recieve timeout 
      binding.Security.Mode = SecurityMode.None; //No security mode 

      //Setup the channel to the service... 
      //TODO debugging use a proper IP address here, and read it from a file. Allows devs to switch between simulator (localhost) and actual embedded DCC 
      channelFactory = new ChannelFactory<IEmbeddedService>(binding, new EndpointAddress(REMOTE_ADDRESS)); 

     } 

     public void Open() 
     { 
      Proxy = channelFactory.CreateChannel(); 
     } 

     public void Close() 
     { 
      channelFactory.Close(); 
     } 
    } 
} 

爲了調試我不斷地跑在我的本地機器上的服務和遠程計算機之間的切換。有沒有辦法從客戶端的app.config中獲取IP,以便在我想更改IP時不必重新編譯?

使用MEX產生app.config客戶端:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <netTcpBinding> 
       <binding name="TCPEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00" 
         receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" 
         transferMode="Buffered" transactionProtocol="OleTransactions" 
         hostNameComparisonMode="StrongWildcard" listenBacklog="10" 
         maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" 
         maxReceivedMessageSize="65536"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" 
           maxArrayLength="16384" maxBytesPerRead="4096" 
           maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
            enabled="false" /> 
        <security mode="None"> 
         <transport clientCredentialType="Windows" 
           protectionLevel="EncryptAndSign"> 
          <extendedProtectionPolicy policyEnforcement="Never" /> 
         </transport> 
         <message clientCredentialType="Windows" /> 
        </security> 
       </binding> 
      </netTcpBinding> 
     </bindings> 
     <client> 
      <endpoint name="TCPEndPoint" 
       address="net.tcp://localhost:9292/EmbeddedService" 
       binding="netTcpBinding" 
       bindingConfiguration="TCPEndPoint" 
       contract="ServiceReference1.IEmbeddedService" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

理想情況下,我只想改變IP這裏。我如何從這裏獲取端點地址?

+1

爲什麼你需要「抓住」地址?爲什麼不使用名稱配置? ChannelFactory不能做到這一點? –

+0

爲什麼不爲IPAddress定義應用程序設置,然後在執行應用程序之前在yourapp.exe.config文件中更改相同的設置? –

+0

@JohnSaunders你能否進一步解釋一下。你的意思是說我不需要'ChannelFactory'來打開服務的頻道? –

回答

7

基本上,你可以做的是創建兩個客戶端的終點 - 一個用於每個你想連接的IP,然後選擇你想要的代碼。

您的客戶端的app.config會是這個樣子:

<client> 
     <endpoint name="tcpLocal" 
      address="net.tcp://localhost:9292/EmbeddedService" 
      binding="netTcpBinding" 
      bindingConfiguration="TCPEndPoint" 
      contract="ServiceReference1.IEmbeddedService" /> 
     <endpoint name="tcpRemote" 
      address="net.tcp://192.168.10.42:9292/EmbeddedService" 
      binding="netTcpBinding" 
      bindingConfiguration="TCPEndPoint" 
      contract="ServiceReference1.IEmbeddedService" /> 
</client> 

,然後在你的代碼,根據某些條件,你就可以選擇使用該tcpLocaltcpRemote客戶端的端點定義:

// connect to the local address 
channelFactoryLocal = new ChannelFactory<IEmbeddedService>("tcpLocal"); 

// or connect to the remote address 
channelFactoryRemote = new ChannelFactory<IEmbeddedService>("tcpRemote"); 

末尾的那些字符串表示在每種情況下使用的<client>/<endpoint>定義的name=。您可以選擇本地或遠程連接 - 或者,如果您喜歡,甚至可以同時使用這兩種連接! :-)

+0

謝謝Marc,這很有道理。 –

3

傳遞端點名稱到的ChannelFactory構造函數,它會查找你的綁定和地址配置爲您提供:

ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>("TCPEndPoint");