所以,我有一个 WCF 服务,它同时在 net.tcp 和 net.pipe 上进行侦听。我已经生成了一个 WCF 代理客户端,我希望它能够通过 tcp 或命名管道进行连接。我不想要 app.config 中的配置,而是代码中的配置。
WCF 客户端将在运行时获取端点地址,例如“net.tcp://mymachine:10001/MyService”或“net.pipe://localhost/MyService”。我认为它只是使用基于 Uri 方案的正确 NetTcpBinding 或 NetNamedPipeBinding - 但它看起来不是那样的。
我不能只设置代理以采用命名管道或 tcp 绑定,它会根据端点地址选择一个吗?
编辑:好的,所以我嗅探方案并填充绑定:
var uri = new Uri("net.tcp://localhost:10001/MyService");
Binding b;
if (uri.Scheme == Uri.UriSchemeNetPipe) {
b = new NetNamedPipeBinding();
} else if (uri.Scheme == Uri.UriSchemeNetTcp) {
b = new NetTcpBinding();
} else if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) {
b = new WSHttpBinding();
}
var proxy = new ClientProxy(b, new EndpointAddress(uri));
但我收到连接失败 - “通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为它处于故障状态。”
如果将 Binding 更改为 BindingElement,并使用带有 CustomBinding 的 NamedPipeTransportBindingElement、TcpTransportBindingElement 等,它可以工作……但我不确定我是否理解其中的区别。