Hi everyone,
How do you get an exception or error code that caused a WCF Client's Faulted event?
private void ConnectionFaultedHandler(object sender, EventArgs e)
{
// Darn! Something went wrong.
// Better clean up.
this._Connection.Abort();
this._Connection.InnerChannel.Faulted -= this.ConnectionFaultedHandler;
// I'd really like to tell the user some detail of what blew up.
// But there is no Exception (or similar) property on the event.
this.NotifyUIOfConnectionFailure(e.Exception);
}
Note, this is similar to this thread, except I 1) couldn't get that way to work and 2) it seems to be addressing the issue on the service side, I'd like to deal with in the client.
EDIT:
To clarify, the above handler is part of a connection that stays open for a long time (hours or even days); it has a callback interface to receive data from the service. I am not asking about exceptions when you call methods like Open or methods that are part of the contract interface, but a fault that occurs because (for example) someone removed a network cable from your PC, or your Internet connection just failed.
Imagine it occurs some time after this code executes:
private void OpenConnection()
{
try
{
this._Connection.Open();
}
catch (Exception ex)
{
// Yes, I should be catching CommunicationsException,
// and TimeoutException, but space is short on StackOverflow.
return;
}
Debug.Assert(this._Connection.State == Open);
this._Connection.InnerChannel.Faulted += this.ConnectionFaultedHandler;
}