我有一個使用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這裏。我如何從這裏獲取端點地址?
爲什麼你需要「抓住」地址?爲什麼不使用名稱配置? ChannelFactory不能做到這一點? –
爲什麼不爲IPAddress定義應用程序設置,然後在執行應用程序之前在yourapp.exe.config文件中更改相同的設置? –
@JohnSaunders你能否進一步解釋一下。你的意思是說我不需要'ChannelFactory'來打開服務的頻道? –