What is a Channel Factory in .NET?

Nevin Mathai picture Nevin Mathai · Mar 19, 2010 · Viewed 15.7k times · Source

What is a Channel Factory and why do you use it?

Answer

marc_s picture marc_s · Mar 19, 2010

If you used Visual Studio's Add Service Reference, or the svcutil.exe tool, you probably won't ever see a ChannelFactory.

Basically, creating the client-side proxy for a WCF service is a two-step process:

  • create the appropriate ChannelFactory<T> for your specific service contract
  • given that channel factory, create the actual communication channel between the client and the service

If you do have control over both ends of the wire, and you can put your service and data contracts into a separate assembly, you can break apart this two step process and handle it manually:

  • create the ChannelFactory<IMyService> once, this is a fairly complex and time-consuming operation, so if ever possible, try to do this only when really necessary, and then cache the channel factory for later reuse

  • create the actual channel using the channel factory whenever you need to communicate with the server

It's a very specific construct for WCF services, so I don't think you'll ever use it outside the WCF scope.