Channel Factory in WCF


A Channel Factory enables you to create a communication channel to the service without a proxy. A Channel Factory that creates and manages the various types of channels which are used by a client to send a message to various configured service endpoints.
A Channel Factory is implemented by the IChannelFactory Interface and their associated channels are used by the initiators of a communication pattern. The Channel Factory class is useful when you want to share a common service contract DLL between the client and the server.

When to use Channel Factory

The Channel Factory class is used to construct a channel between the client and the server without creating a Proxy. In some of the cases in which your service is tightly bound with to the client application, we can use an interface DLL directly and with the help of a Channel Factory, call a method. The advantage of the Channel Factory route is that it gives you access method(s) that wouldn't be available if you use svcutil.exe.
Channel Factory is also useful when you do not share more than just a service contract with a client. If you know that your entity will not change frequently than it is better to use a Channel Factory than a Proxy.
Using the ChannelFactory<T> class is an easier alternative to making calls to the WCF services to the laborious process of generating proxies via the SvcUtil.exe tool every time a service contract changes. Channel Factory is a factory for creating service communication channels at runtime.
ChannelFactory<T> takes a generic parameter of the service type (it must be a service contract interface) to create a channel. Because ChannelFactory only requires knowledge of the service contract, it makes good design sense to put service/data contracts in separate assemblies from service implementations. This way, you can safely distribute contract assemblies to third parties who wish to consume services, without the risk of disclosing their actual implementation.
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  5.     [OperationContract]  
  6.     string GetData(int value);  
  8.     [OperationContract]  
  9.     CompositeType GetDataUsingDataContract(CompositeType composite);  
  11.     // TODO: Add your service operations here  
  12. }  

How to Call WCF Service Use Channel Factory

The first step is to call the WCF Service and create a service contract in your application; please make sure that the method name, parameters and return type are identical to the actual service.
To do this we require the actual End Point Address.
  1. BasicHttpBinding myBinding = new BasicHttpBinding();  
  2. EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3047/Service1.svc");  
  3. ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);  
  5. IService1 instance = myChannelFactory.CreateChannel();  
  6. // Call Service.  
  7. Console.WriteLine(instance.GetData(10));  
  9. myChannelFactory.Close();  


Channel Factory is useful when you do not share more than just the service contract with the client. And also your service entity will not change frequently. You can also cache your channel using the static property.

Similar Articles