tags:

views:

946

answers:

2

Hi,

How can I keep my WCF Service Client Connected with WinForm even if a Faulted State appened ?

Thanks.

+1  A: 

As far as I know, a faulted state is usually terminal to a WCF proxy. So no, I don't think so.

Marc Gravell
+3  A: 

Answer myself :)

You might subscribe to InnerChannel Events

            svc.InnerChannel.Closed += InnerChannel_Error;
            svc.InnerChannel.Closing += InnerChannel_Error;
            svc.InnerChannel.Faulted += InnerChannel_Error;

Then Handle Exceptions and Recreate the Service Proxy

private void InnerChannel_Error(object sender, EventArgs e)
{
    var svc = _entrepotService as EntrepotServiceProxy;
    try
    {
        if (svc != null)
        {
            if (svc.State != CommunicationState.Faulted)
            {
                svc.Close();
            }
            else
            {
                svc.Abort();
            }
        }
    }
    catch (CommunicationException)
    {
        if (svc != null) svc.Abort();
    }
    catch (TimeoutException)
    {
        if (svc != null) svc.Abort();
    }
    catch
    {
        if (svc != null) svc.Abort();
        throw;
    }
    _entrepotService = new EntrepotServiceProxy();
}
Yoann. B
(just to clarify) That is not exactly keeping the connection open (as Marc said, faulted state implies (usually) the connection is closed. This is recreating a connection on the close event.
Russell