Always use ChannelFactory when referencing WCF services (particularly for Azure)

Given the indeterminate nature of the IP addresses of Azure web roles (There are a number of exceptions to this but in general) you end up in a real pickle if you you use the Service Reference functionality built into Visual Studio that most of us regularly do.

(This thing)


Truthfully, the way the proxy service references have always worked had its flaws despite its convenience.

The problem boils down to two things. First, if you’re correctly using WCF services you should be binding to DataContracts and Interfaces rather than any concrete implementations anyway. Secondly, you’re stuck with hard-coded values that live in your web.config, with what is inherently dynamic and often not known at compile time (ip addresses).

So how do you solve it?

Remarkably simply, it takes the form of the channel factory and provided you have both your datacontracts and your service interfaces referenced you can then do this.


Download the example.