Using ChannelFactory Vs. Proxies in WCF

I tried researching this area over the net, but couldn't find a whole lot. So, here's my blog about it and since this is my very first blog, I'd like to say "Hello World!"

What does it mean to use ChannelFactory?

When you share a common service contract dll between the client and the server, you'll be using the ChannelFactory class. The idea is to package the service contract interface and your entities in a library, that would be implemented by the service and used by the client.

So if you have a contract such as this in Contract.dll:

 public interface IHelloWorld{

string SayHello(int numTimes);

}

You'd use the ChannelFactory class as follows in the client:

using Contract; 

ChannelFactory<IHelloWorld> channel = new ChannelFactory<IHelloWorld>("tcp"); //Binding name

When to use a proxy?

We use proxy in WCF to be able to share the service contract and/or entities with the client. If the client to your service is external to the system, such as API, it makes sense to use a proxy, because it makes sharing the contract easier by giving a code file rather than a DLL. 

When to use a ChannelFactory i.e. a shared DLL?

A DLL is helpful if the client code is under you control and you'd like to: share more than just the service contract with the client -- such as some utility methods associated with entities and make the client & the service code more tightly bound. If you know that your entities will not change much and the client code is less, then a DLL would work better than a proxy. Proxies have several restrictions like:

  1. Properties need to have gets and sets
  2. Contructors can't be exposed
  3. Methods other than the service contract cannot be exposed

What all to package in the DLL?

You'll need to package the following:

  1. Entities
  2. Service contract -- the interface
  3. RequestMsg classes (if any)
  4. ResponseMsg classes (if any)

So if you are designing a connected system and using WCF for that, then you can use a shared dll instead of a proxy, to avoid code repetition and be more effective in the use of the service entities.