Search code examples
.netwcfweb-servicesexceptionfault

Why do my WCF service returning a FaultException, time out after 10 calls?


I have a WCF service that sometimes has to return a Fault. For some reason, the calls to my service begins to time out with the following error: "The request channel timed out while waiting for a reply after 00:00:59.8906201. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout."

After examining the problem, a pattern emerged: When the service had returned a fault 10 times, the timeout starts. So I created a testservice implemented by:

public string GetData(int value)
{
    throw new FaultException("A testerror occured");
}

And a testclient:

   protected void RunTestGetData()
    {
        using (TestServiceReference.Service1Client client
            = new WSPerformanceTester.TestServiceReference.Service1Client())
        {
            try
            {
                client.GetData(1);
                client.Close();
                outputWriter.WriteLine(string.Format("Call run in thread {0}: GetData()", Thread.CurrentThread.ManagedThreadId));
                outputWriter.Flush();
            }
            catch (Exception e)
            {
                client.Abort();
                client.Close();
                outputWriter.WriteLine(string.Format("Error occured in thread {0}: GetData(): {1}", Thread.CurrentThread.ManagedThreadId, e.Message));
                outputWriter.Flush();
            }
        }
    }

This only happens, when the service is returning a FaultException. If I throw a normal exception, the service is able to continue running after the 10th call. Obviously, i would like to wrap my exceptions nicely, so just throwing normal exceptions is not a real option.

Why do I experience these timeout exceptions? Thanks in advance for any help..


Solution

  • Apparently, the client code should be as follows:

    protected void RunTestGetData()
    {
        TestServiceReference.Service1Client client
            = new WSPerformanceTester.TestServiceReference.Service1Client()
        try
        {
            client.GetData(1);
        }
        catch (FaultException e)
        {
            //Handle fault
        }
        try
        {
            if (client.State != System.ServiceModel.CommunicationState.Faulted)
            {
                client.Close();
            }
        }
        catch(Exception e)
        {
            outputWriter.WriteLine("Error occured in Client.Close()");
            outputWriter.Flush();
            client.Abort();
        }
    }
    

    Calling client.Abort() should always be a last resort.