Click here to Skip to main content
14,037,792 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 11 Apr 2014
Licenced CPOL

Dynamic Generation of Client Proxy at Runtime in WCF using a Single Factory

, 11 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Dynamic Generation of Client Proxy at Runtime in WCF using a Single Factory


In conventional method, if client proxy for a WCF service is required to be generated at runtime programmatically, an instance of ChannelFactory (generic type) is created passing the interface type of the service (contract) as parameter to the generic class. This requires different implementation for different services for generating client proxy which reduces the generic scope.

This article describes a method to create a factory class which generates client proxy at runtime from the type of the service contract received as parameter. This will eliminate separate implementation requirement by using a single factory class with the help of .NET reflection.


For example, we have the following Service and Service Contract for a WCF service:

interface IMyService
    void DoSomething();

public class MyService : IMyService
    public void DoSomething()
        // do something

Conventional Method of Client Proxy Generation at Runtime

To generate client proxy in conventional method is to create the ChannelFactory with a prior knowledge of Service contract and this will require separate implementation for each service in the system:

public class Test
    public void Test()
        BasicHttpBinding myBinding = new BasicHttpBinding();
        EndpointAddress myEndpoint = new EndpointAddress("http://localhost/MyService/");
        ChannelFactory<imyservice> myChannelFactory = new ChannelFactory<imyservice>(myBinding, myEndpoint);
        IMyService pClient = myChannelFactory.CreateChannel();

Factory Method of Client Proxy Generation at Runtime

The following method will eliminate the above deficiency by providing a client factory class. This will help in generating client proxy in a more generic way using the single factory:

public class Test
    public void Test()
        //create client proxy from factory
        IMyService pClient = (IMyService)ClientFactory.CreateClient(typeof(IMyService));
//Factory class for client proxy
public abstract class ClientFactory
    public static object CreateClient(Type targetType)
        BasicHttpBinding binding = new BasicHttpBinding();
        //Get the address of the service from configuration or some other mechanism - Not shown here
        EndpointAddress addess = new EndpointAddress(GetAddessFromConfig(targetType));
        //dynamic factory generation
        object factory = Activator.CreateInstance(typeof
        (ChannelFactory<>).MakeGenericType(targetType), binding, address);
        MethodInfo createFactory = factory.GetType().GetMethod("CreateChannel", new Type[] { });
        //now dynamic proxy generation using reflection
        return createFactory.Invoke(factory, null);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Krishnendu Ghosh
Software Developer Microsoft
India India
No Biography provided

You may also be interested in...


Comments and Discussions

SuggestionThis is refactoring not dynamic client proxy generation Pin
tinu7318-Dec-15 23:11
membertinu7318-Dec-15 23:11 
SuggestionInclusion of Method Pin
stixoffire2-Nov-15 10:22
memberstixoffire2-Nov-15 10:22 
QuestionDo I need to add a reference to the assembly of the service project? Pin
abhi_here2-Feb-15 4:55
memberabhi_here2-Feb-15 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190425.1 | Last Updated 11 Apr 2014
Article Copyright 2014 by Krishnendu Ghosh
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid