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;
3: public static IAuthenticateService GetClient()
5: if (AuthChannelFactory == null)
6: AuthChannelFactory = new ChannelFactory<IAuthenticateService>();
7: var client = AuthChannelFactory.CreateChannel();
9: (client as ICommunicationObject).Faulted += new EventHandler(AuthChannel_Faulted);
10: return client;
In the event handler, re-create the channel using the channel factory:
1: static void AuthChannel_Faulted(object sender, EventArgs e)
3: (sender as ICommunicationObject).Abort();
4: sender = AuthChannelFactory.CreateChannel();
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
Learn more about Tallan or see us in person at one of our many Events!