从 app.config 获取 WCF 客户端端点 IP

发布于 2024-12-08 17:49:11 字数 3711 浏览 1 评论 0原文

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

I have a client that connects to a WCF service using a netTcpBinding.

To connect to the service I use the following in my client:

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();
        }
    }
}

For debugging I constantly switch between running the service on my local machine and a remote machine. Is there a way to grab the IP from the client's app.config so that I do not have to recompile whenever I want to change the IP?

The client app.config is generated using 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>

Ideally, I would just change the IP here. How can I grab the endpoint address from here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-12-15 17:49:11

基本上,您可以做的是创建两个客户端端点 - 每个端点对应您想要连接的每个 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= 。您可以选择本地或远程连接 - 或者,如果您愿意,甚至可以同时使用两者! :-)

Basically, what you could do is create two client-side endpoints - one for each IP you want to connect to, and then pick which one you want in your code.

Your client's app.config would look something like this:

 <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>

and then in your code, based on some criteria, you would have to use either the tcpLocal or the tcpRemote client-side endpoint definition:

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

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

Those strings at the end denote the name= for the <client>/<endpoint> definition to use in each case. You can pick the local or the remote connection - or heck, even have both available at the same time, if you like! :-)

夜访吸血鬼 2024-12-15 17:49:11

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

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

Pass the endpoint name to the ChannelFactory constructor and it will look up your binding and address from config for you:

ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>("TCPEndPoint");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文