C# 从app.config获取WCF客户端终结点IP
我有一个客户端,它使用C# 从app.config获取WCF客户端终结点IP,c#,wcf,app-config,C#,Wcf,App Config,我有一个客户端,它使用netTcpBinding连接到WCF服务 要连接到服务,我在客户端中使用以下命令: namespace Embedded_DCC_Client { public class EmbeddedClient { private ChannelFactory<IEmbeddedService> channelFactory; //Embedded DCC TCP Addresses public co
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();
}
}
}
理想情况下,我会在这里更改IP。如何从这里获取端点地址?将端点名称传递给ChannelFactory构造函数,它将从配置中查找您的绑定和地址:
ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>("TCPEndPoint");
ChannelFactory ChannelFactory=新的ChannelFactory(“TCPEndPoint”);
将端点名称传递给ChannelFactory构造函数,它将从配置中为您查找绑定和地址:
ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>("TCPEndPoint");
ChannelFactory ChannelFactory=新的ChannelFactory(“TCPEndPoint”);
基本上,您可以创建两个客户端端点—每个要连接的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>
末尾的字符串表示在每种情况下使用的
/
定义的名称=
。您可以选择本地或远程连接—或者,如果您愿意,甚至可以同时使用这两种连接!:-) 基本上,您可以做的是创建两个客户端端点—每个要连接的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>
末尾的字符串表示在每种情况下使用的
/
定义的名称=
。您可以选择本地或远程连接—或者,如果您愿意,甚至可以同时使用这两种连接!:-) 你为什么要“抢”这个地址?为什么不按名称使用配置?ChannelFactory不能这样做?为什么不为IPAddress定义一个应用程序设置,然后在执行应用程序之前在yourapp.exe.config文件中更改相同的设置?@johnsaunder您可以进一步解释一下。你的意思是我不需要ChannelFactory
来打开该服务的频道吗?@jayanadey谢谢你的回复,我不知道该怎么做。你能举个例子吗?事实上,试着使用newchannelfactory(“TCPEndPoint”)
为什么需要“抓取”地址?为什么不按名称使用配置?ChannelFactory不能这样做?为什么不为IPAddress定义一个应用程序设置,然后在执行应用程序之前在yourapp.exe.config文件中更改相同的设置?@johnsaunder您可以进一步解释一下。你的意思是我不需要ChannelFactory
来打开该服务的频道吗?@jayanadey谢谢你的回复,我不知道该怎么做。你能举个例子吗?事实上,试着使用newchannelfactory(“TCPEndPoint”)
谢谢Marc,这很有道理。谢谢Marc,这很有道理。