Search code examples
c#wcftimeoutwcf-binding

WCF how can I avoid faulted channels if they are closed by server due to inactivity


My service host is configured to close connection after 10 mins

<wsHttpBinding>
    <binding receiveTimeout="00:10:00">
</wsHttpBinding>

In my client code before using service it is validated. If it is in closed/faulted state it will create a new one.

    public static T ValidateService<T>(ref T service) where T : class
    {
        if (service is IClientChannel)
        {
            IClientChannel channel = service as IClientChannel;
            CommunicationState state = channel.State;
            if (state == CommunicationState.Faulted || state == CommunicationState.Closed)
            {
                // thread safe logic to create new service 
            }
        }
    }

Problem is when channel is closed on server side and channel.State will still return Open state. After I do remote call I get faulted exception and due to receiveTimeout.

Is there any way to have inactivity timeout on client side? something like receiveTimeout="00:10:00" on server but with few seconds less so I can nicely close a channel from client if not used and than my ValidateService<T> logic will create a new one if needed.

For performance reasons I do not want to create new channel for every remote call because those can be very frequent.

I would like to avoid having heart beat poller for each of my services I am using in my communication.

Thanks


Solution

  • You can't avoid handling Faulted and Closed since it is always possible that some network problem or similar leads to a situation where you have to create a new Channel...

    EDIT - as per comment:

    In production I usually have a client implementation which handles certain exceptions by recreating the Channel and retrying... this takes care of the case you are describing and several other cases like network hickup etc.