Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Recover from a WCF Service Fault

I’ve been looking for an elegant way to recover from a WCF Service Client’s channel going into a Faulted state, and after searching for some time came across a great simple quick and dirty example:

When creating your WCF Service Client, add an event handler to the ICommunicationObject.Faulted event on your service client:

   1: private static ChannelFactory<IAuthenticateService> AuthChannelFactory = null;

   2:

   3: public static IAuthenticateService GetClient()

   4: {

   5:     if (AuthChannelFactory == null)

   6:         AuthChannelFactory = new ChannelFactory<IAuthenticateService>();

   7:     var client = AuthChannelFactory.CreateChannel();

   8:

   9:     (client as ICommunicationObject).Faulted += new EventHandler(AuthChannel_Faulted);

  10:     return client;

  11: }

 

In the event handler, re-create the channel using the channel factory:

   1: static void AuthChannel_Faulted(object sender, EventArgs e)

   2:        {

   3:            (sender as ICommunicationObject).Abort();

   4:            sender = AuthChannelFactory.CreateChannel();

   5:        }

You can re-use the AuthChannel_Faulted event by checking the object type and using the correct ChannelFactory based on type.

Method obtained from : Nahid’s Blog

1 Comment. Leave new

sender = AuthChannelFactory.CreateChannel();

Why setting this parameter? it is not passed by ref !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>