Dynamic WCF Usage in Client

Once a Windows Communication Foundation (WCF) proxy has been created and configured, a client instance can be created and the client application can be compiled and used to communicate with the WCF service. This topic describes procedures for instantiating and using a WCF client. This procedure does three things:

1.   Instantiates a WCF client.

2.   Calls the service operations from the generated proxy.

3.   Closes the client once the operation call is completed.

Why am I using Channel Factory instead of proxy?

Using Proxy: The first benefit of proxy usage is very simple, it is to develop a WCF service capable of fast communication also developing a fast client application. A proxy knows everything about the WCF service life cycle auto generated proxy talking WCF service using WSDL. Don't forget; if you generate a technology, you will depend on it. An advantage of the proxy is that you can give all the hard work to Visual Studio. VS helps you to generate a proxy. If the service address changes for any reason then you need to regenerate it. You depend on Visual Studio that takes all control from you.

Using Channel Factory: The first benefit of Channel Factory is it is easy to change when you need new attractions on services. First you describe the service contract, second create the Channel Factory and than call over that channel. You can control everything without the VS wizard.

By Creating Generic Service Factory

In small projects, you can directly call the ChannelFactory but remember tight coupling in large projects is a big handicap. Loose coupling is necessary to control services methods more precisely. You need a manager class to create channels as specified by the single responsible principle. You should develop  a service factory as in the following:

public static class ServiceFactory    
private static readonly ClientSection _clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
private static readonly BindingsSection _bindingSection = ConfigurationManager.GetSection("system.serviceModel/bindings") as BindingsSection;
public static T Create<T>()
             T context = default(T);
             foreach (ChannelEndpointElement endpoint in _clientSection.Endpoints)
                 if (endpoint.Contract == typeof(T).FullName)
                     Binding b = BindingFactory.GetFromConfig(endpoint.BindingConfiguration);
                     context = ChannelFactory<T>.CreateChannel(b, new EndpointAddress(endpoint.Address));
             return context;
public static class BindingFactory
             public static Binding GetFromConfig(string configurationName)
                 var bingingsSection = BindingsSection.GetSection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
                 var bindingType = (from b in bingingsSection.BindingCollections
                                    where b.ConfiguredBindings.Count > 0 && b.ContainsKey(configurationName)
                                    select b.BindingType).FirstOrDefault();
                 var binding = bindingType != null ? Activator.CreateInstance(bindingType, configurationName) : null;
                 return (Binding)binding;

Let's see the Class Diagram. You can see the stock market WCF Hierarchy.


 Service Factory hierarchy below.



IBook bookproxy = ServiceFactory.Create<IBook>();            
            var result = bookproxy.GetBook(1);
            Console.WriteLine(string.Format("result= {0}", result.ToString()));
IMusic musicproxy = ServiceFactory.Create<IMusic>();
            var result2 = musicproxy.GetAlbum(2);
            Console.WriteLine(string.Format("result= {0}", result2.ToString()));
IFilm filmproxy = ServiceFactory.Create<IFilm>();
           var result3 = filmproxy.GetFilm(1);
           Console.WriteLine(string.Format("result= {0}", result3.ToString()));



You've learned how to use Channel Factory by using the generic Service Factory. Thanks to Channel Factory, you can access the assembly directly. If you would like to take control of the service layer then you can choose Channel Factory because of more flexibility and maintainability, Yes, Channel Factory is not easier than a Proxy but that would work better than a proxy.

Up Next
    Ebook Download
    View all
    View all