Why would one use the ChannelFactory to instantiate a WCF proxy rather than a Service Reference?

There seem to be two ways to instantiate WCF service proxies described here. My question is why would one want to use the ChannelFactory to instantiate a WCF proxy and what are the benefits of this approach?

I've met people with a strong opinion on the second option but I cannot manage to understand a clear argumentation from them


If you are using assembly-sharing (which is, let's face it, hugely convenient if you own both ends of the pipe), then both ends already have a complete version if the interface etc. It seems redundant to run the svcutil tool (at command-prompt or via the IDE) just to get back a wrapper for what we already have. It also works very well when you want to code only to an interface, not to the wrapper class - and use some centralised code to get a channel to a particular interface at a given address. This is actually how I mainly use WCF.

The "client" approach works well for one-off services, or where it doesn't need to go through any central tooling. It also makes for very simple demos.

The first option uses configuration settings provided in the web.config / app.config files to instantiate a proxy, however in certain situations its not feasible to put these settings in that file, for example if your application needs to use a different binding (maybe HTTP vs Named Pipes) depending on the scenario, or possibly your application may not even have a .config file.

The second option gives a lot more flexibility when creating proxies to programatically specify the configuration to use for each proxy as you instantiate it.

To give a more concrete example, supposing you wish to use Named Pipes for communication if communicating with the local machine, and HTTP if communicating with a remote host:

if (UseNamedPipes())
    EndpointAddress address = new EndpointAddress("net.pipe://localhost/Simple/BankService");
    return ChannelFactory<IBank>.CreateChannel(new NetNamedPipeBinding(), address);
    EndpointAddress address = new EndpointAddress("http://localhost:8000/Simple/BankService");
    return ChannelFactory<IBank>.CreateChannel(new BasicHttpBinding(), address);

Need Your Help

How are very large Java EE web sites structured?

java deployment java-ee

A best practice question - how are very large websites best structured with Java.

CONCAT function in MySQL using PHP Undefined Variable

php mysql wamp wampserver

I need help on how to do this correctly. I need to execute this command:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.