Question

How to configure WCF to use just one of two available endpoints?

I need two TCP ports (and thus using netTcpBinding). The service host should first try to bind to the first port. If it fails, and only if it fails, it should try to bind to the second port.

EDIT

I known it can be achived programatically, but my intention to do it declaratively (using .config files only).

Was it helpful?

Solution

The endpoint address, including the port number, can be set in code at any point in the process before you open a connection using your proxy object. So you can set the address and then test the connection, and if it fails, try the other port. Here's some code that hopefully illustrates my point.

Dim oProxy as New YourWCFServiceType()

oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try first"))

Dim FirstBindingSucceeded as Boolean
Try
    oProxy.Open()
    FirstBindingSucceeded = True
Catch
End Try

If FirstBindingSucceeded = False Then
    oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try second"))
End If

oProxy.Open()

OTHER TIPS

On the server side there is no problem exposing a service with two bindings.

But on the client side you will get a duplicate contract error (or words to that effect)

One way to do it is to create two interfaces (contracts) that are identical except for the name.

You have a single copy of the implementation, each service inherits from this implementation.

You then have two services on different ports, that have the same implementation / functionality.

On the client you then need to program that it first attempts the first port and then if that fails it attempts the second.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top