views:

317

answers:

2

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

A: 

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.

Shiraz Bhaiji
+1  A: 

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()
Jacob Ewald
I would like to do it declaratively.
Michael Damatov
I still think my answer addresses your question. The endpoint defined in your .config file is just a default. If you want to select between two different addresses/ports then just add a setting to the config and read it in when you need it.<appSettings> <add key="FailOverURL" value="the url" /></appSettings>In my opinion, the amount of overhead involved with switching between contract types described by Shiraz Bhaiji would greatly degrade the benefits of having two endpoints defined by default. What happens when you need a third port?
Jacob Ewald