简体   繁体   中英

Keep WCF Service Connected

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

Thanks.

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM