【问题标题】:Get WCF client endpoint IP from app.config从 app.config 获取 WCF 客户端端点 IP
【发布时间】:2011-12-03 05:21:21
【问题描述】:

我有一个使用 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。如何从这里获取端点地址?

【问题讨论】:

  • 为什么要“抢”地址?为什么不直接按名称使用配置? ChannelFactory 做不到?
  • 为什么不为 IPAddress 定义一个应用程序设置,然后在执行应用程序之前在 yourapp.exe.config 文件中进行更改?
  • @JohnSaunders 你能进一步解释一下吗?你的意思是我不需要ChannelFactory 来打开服务的通道?
  • @JayantaDey 感谢您的回复,我不知道该怎么做。你能举个例子吗?
  • 其实用new ChannelFactory&lt;IMyService&gt;("TCPEndPoint")试试

标签: c# wcf app-config


【解决方案1】:

基本上,您可以做的是创建两个客户端端点 - 一个用于您要连接的每个 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");

末尾的那些字符串表示name=,以便在每种情况下使用&lt;client&gt;/&lt;endpoint&gt; 定义。您可以选择本地或远程连接 - 或者,如果您愿意,甚至可以同时使用两者! :-)

【讨论】:

  • 谢谢 Marc,这很有道理。
【解决方案2】:

将端点名称传递给 ChannelFactory 构造函数,它将为您从配置中查找绑定和地址:

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

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2010-09-10
    • 2012-09-02
    • 2011-11-23
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多