[英]Get WCF client endpoint IP from app.config
我有一個使用netTcpBinding
連接到WCF服務的客戶端。
要連接到服務,請在客戶端中使用以下命令:
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時都重新編譯?
客戶端app.config
使用MEX生成:
<?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。 如何從此處獲取端點地址?
基本上,您可以做的是創建兩個客戶端端點-一個您要連接的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>
然后在代碼中,基於某些條件,您將必須使用tcpLocal
或tcpRemote
客戶端端點定義:
// connect to the local address
channelFactoryLocal = new ChannelFactory<IEmbeddedService>("tcpLocal");
// or connect to the remote address
channelFactoryRemote = new ChannelFactory<IEmbeddedService>("tcpRemote");
最后的那些字符串表示在每種情況下使用的<client>/<endpoint>
定義的name=
。 您可以選擇本地或遠程連接-或選擇兩者,如果願意,甚至可以同時使用兩者! :-)
將端點名稱傳遞給ChannelFactory構造函數,它將為您從config中查找您的綁定和地址:
ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>("TCPEndPoint");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.