Introduction
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 massage to various configured service end points.
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 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 the 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.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
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.
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3047/Service1.svc");
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);
IService1 instance = myChannelFactory.CreateChannel();
// Call Service.
Console.WriteLine(instance.GetData(10));
myChannelFactory.Close();
Conclusion
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.