views:

612

answers:

1

So, I have a WCF service that is listening on both net.tcp and net.pipe. I've generated a WCF proxy client, and I'd like it to be able to connect via tcp or named pipe. I don't want the config in app.config, but in code.

The WCF client will get the endpoint address at runtime, so something like "net.tcp://mymachine:10001/MyService" or "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.

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?

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

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.

+2  A: 

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.

casperOne
I've got it working with the *TransportBindingElements and a custom binding, but I'm confused as to what the difference between that and the Bindings are....
Mark Brackett