2011-10-07 134 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");