开发者

How do I get the cause of a WCF Client Faulted Event?

开发者 https://www.devze.com 2022-12-12 16:38 出处:网络
How do you get an exception or error code that caused a WCF Client\'s Faulted event? private void ConnectionFaultedHandler(object sender, EventArgs e)

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 addr开发者_如何学编程essing 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;
}


Your question is not so clear, by the way

Receiving Fault is different from receiving exception.

whenever you throws an exception object that will be received on client with corrosponding catch block.

Fault is received in case of channel problem(not connected)...

0

精彩评论

暂无评论...
验证码 换一张
取 消