简体   繁体   English

WCF绑定

[英]WCF Bindings

So, I have a WCF service that is listening on both net.tcp and net.pipe. 因此,我有一个正在监听net.tcp和net.pipe的WCF服务。 I've generated a WCF proxy client, and I'd like it to be able to connect via tcp or named pipe. 我已经生成了一个WCF代理客户端,我希望它能够通过tcp或命名管道进行连接。 I don't want the config in app.config, but in code. 我不想在app.config中配置,但在代码中。

The WCF client will get the endpoint address at runtime, so something like "net.tcp://mymachine:10001/MyService" or "net.pipe://localhost/MyService". WCF客户端将在运行时获取端点地址,因此类似“ net.tcp:// mymachine:10001 / MyService”或“ net.pipe:// localhost / MyService”。 I would think that it'd just use the correct NetTcpBinding or NetNamedPipeBinding based off the Uri scheme - but it doesn't look that way. 我认为它只是基于Uri方案使用正确的NetTcpBinding或NetNamedPipeBinding-但它看起来不是那样。

Can't I just set up the proxy to take either named pipe or tcp bindings, and it'll choose the one based on the endpoint address? 我不能仅将代理设置为采用命名管道或tcp绑定,而是根据端点地址选择一个吗?

EDIT: Okay, so I sniff the scheme and populate the binding: 编辑:好的,所以我嗅探该方案并填充绑定:

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

but I get a connection failure - "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state." 但出现连接失败- “通信对象System.ServiceModel.Channels.ServiceChannel无法用于通信,因为它处于故障状态。”

If change Binding to a BindingElement, and use NamedPipeTransportBindingElement, TcpTransportBindingElement, etc. with a CustomBinding it works...but I'm not sure I understand what the difference is. 如果将Binding更改为BindingElement,并将NamedPipeTransportBindingElement,TcpTransportBindingElement等与CustomBinding一起使用,则可以使用...但是我不确定我知道有什么区别。

No, you can't. 不,你不能。 There can only be one transport element in the binding, having more than one just doesn't make sense. 绑定中只能有一个传输元素,而没有多个元素是没有意义的。

You will have to look at the scheme, and then based on that, choose the correct binding. 您将必须查看该方案,然后基于该方案选择正确的绑定。 It's simple enough to do, using the Uri class, which will perform the parsing for you. 使用Uri类可以很简单地完成,它将为您执行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM