When a single ClientBase<T>
instance is used for multiple WCF service calls, it can get a channel into a faulted state (ie. when the service is down).
I would like to heal the channel automatically when the service comes up again. The only way I found is to call the following code before each method call:
if (clientBase.InnerChannel.State == CommunicationState.Faulted)
{
clientBase.Abort();
((IDisposable)clientBase).Dispose();
clientBase = new SampleServiceClientBase();
}
I got the feeling that this isn't the right way to do it. Anyone got a better idea?
You can't. Once a channel is faulted, it's faulted for good. You must create a new channel. WCF channels are stateful (in a manner of speaking), so a faulted channel means the state may be corrupted.
What you can do is put the logic you're using into a utility method:
Then you can invoke your service with the following:
Or if you want to use the default parameterless constructor, just:
Since it also handles the case where the service is
null
, you don't need to initialize the service before calling it.Pretty much the best I can offer. Maybe somebody else has a better way.