Click here to Skip to main content
13,300,331 members (52,790 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 8 Nov 2010

A Better Way for a Client to Work with WCF Services

, 8 Nov 2010
Rate this:
Please Sign up or sign in to vote.
A component which allows to work with WCF services in a robust way


In my opinion, Microsoft failed with the implementation of the generic ClientBase implementation that is used for connecting with services. The channel contained within the client base implementation will become invalid when a problem occurs during the communication with the service.

The downside is that this channel cannot be revived so that it can be reused. The only solution is to dispose the current instance and to recreate a new one. This will put a strain on the developers since they need to implement the code for handling the invalid channel and for recreating the actual client proxy.

As one can imagine, this is not efficient and can cause problems since each client proxy needs to be implemented in their own way. Some functionality can be copied and pasted, but this will be error prone.

Other Solutions

I did some browsing to see if others ran into the same problems. The only interesting solution I found was at and shows a dynamic proxy which is generated at runtime. This was almost exactly what I had been looking for. The solution however was too complex for my taste, so I started on my own version while reusing certain ideas and the intermediate language generation.

The solution to the problem is to encapsulate the actual client proxy, which is based on the ClientBase, in a specific class which will contain the functionality to recreate the proxy in case of problems. The problem however is that the calls present on the service interface need to be extended with a way to detect these problems. To be able to handle this in a generic way, we need to generate this functionality at runtime to prevent that we still need actions to keep the implementation up to date. This can be done by creating a temporary assembly which will contain the implementation for the specific implementation of the interface by generating intermediate language.

So a generic ProxyClassBuilder is available which will generate a type that contains the methods which will handle the calls to the service implementation. Each method will contain functionality for catching exceptions and for handling them accordingly.

Code Generation

I will explain shortly what happens when the following service contract is being used:

public interface ITestService
     void DummyCall();

The following will trigger the creation of the specific client proxy which includes the method and the implementation that will handle any exceptions that occur.

clientProxy = ClientProxy<itestservice />.GetInstance("identifier", binding, endpointAddress);

The client proxy that is returned is derived from the ClientProxy class and will contain the following implementation:

public class TestService : ClientProxy<itestservice />, ITestService
    // Methods
    public TestService(Binding binding1, EndpointAddress address1) : 
        base(binding1, address1)

    public void DummyCall()
        catch (Exception exception)

It will pass the method call onto the enclosed channel which will eventually call the service. Any exceptions are caught and passed onto the HandleException method which resides in the base class.

Classes Involved

The generic ClientProxy is the entry point and the one that is to be used by the consumer of the service. It has an abstract method GetInstance which is used to get an instance of the generated class. This generated class is completely built by the ProxyClassBuilder. For performance issues, the ClientProxy will contain a cache with already generated client proxies. This ensures that the communication with a certain service within the same client is done via one instance of the proxy.

The generic ProxyClassBuilder is responsible for the generation of a temporary type. This type is cached internally to prevent that it needs to be generated more than once.

The generic InternalClientProxy is the actual proxy that talks with the service. This one is recreated when the connection with the server is somehow severed.

The Enclosed Solution

The enclosed solution contains 3 projects:

  • The test application with a simple user interface which supports that starting/stopping of the service, calling the methods on the client proxy and logging some information regarding the process.
  • The test service itself, it contains both the interface and the implementation. This is not the way to go, but hey it is just an example.
  • The actual implementation for the dynamic client proxy. This contains the interesting part with the generation of the client proxy by using the intermediate language.


  • 8 Nov 2010 - First version


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


About the Author

JP van Mackelenbergh
Architect Altran
Netherlands Netherlands
I am a relaxed guy, born in '73, who likes to develop software professionally in both my work and in my spare time.

Keywords are C#, WCF, WPF.

You may also be interested in...


Comments and Discussions

QuestionGive away offer Pin
alexferri00110-Aug-12 6:39
memberalexferri00110-Aug-12 6:39 
QuestionHow would you deploy the DLL to a website? Pin
Bill SerGio, The Infomercial King2-Nov-11 6:13
memberBill SerGio, The Infomercial King2-Nov-11 6:13 
GeneralBetter if used with dependency injection Pin
matthewadams16-Nov-10 5:27
membermatthewadams16-Nov-10 5:27 
GeneralRe: Better if used with dependency injection Pin
JP van Mackelenbergh16-Nov-10 7:44
memberJP van Mackelenbergh16-Nov-10 7:44 
Questioncan you clarify. Pin
vrajaraman15-Nov-10 20:18
membervrajaraman15-Nov-10 20:18 
AnswerRe: can you clarify. Pin
JP van Mackelenbergh15-Nov-10 21:31
memberJP van Mackelenbergh15-Nov-10 21:31 
GeneralRe: can you clarify. Pin
vrajaraman15-Nov-10 23:28
membervrajaraman15-Nov-10 23:28 
also look this earlier sample from Microsoft[^]
GeneralRe: can you clarify. Pin
JP van Mackelenbergh16-Nov-10 0:53
memberJP van Mackelenbergh16-Nov-10 0:53 
GeneralYou got my 5 Pin
povp9-Nov-10 6:52
memberpovp9-Nov-10 6:52 
GeneralRe: You got my 5 Pin
JP van Mackelenbergh10-Nov-10 5:01
memberJP van Mackelenbergh10-Nov-10 5:01 

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 | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by JP van Mackelenbergh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid