Search code examples
c#.netwcftimeoutbasichttpbinding

Why is the WCF Client timeout ignored


I have a WCF client with the following piece of code written:

MyClient myClient = new MyClient();

string id = Guid.NewGuid();

string result = myClient.Foo(id);

Console.WriteLine(result);

This works, but I want to add a time limit for the service call, so an exception will be thrown if the operation takes too long. I tried adding the timeout in the config file at the binding element like so:

<basicHttpBinding>
   <binding 
       receiveTimeout="00:00:05"
       sendTimeout="00:00:05"
   </binding>
</basicHttpBinding>

This doesn't seem to work sadly.

I also tried setting it manually in the code file like so:

MyClient myClient = new MyClient();

myClient.Endpoint.Binding = new BasicHttpBinding()
{
    SendTimeout = new TimeSpan(0, 0, 5),
    ReceiveTimeout = new TimeSpan(0, 0, 5)
};

string id = Guid.NewGuid();

string result = myClient.Foo(id);

Console.WriteLine(result);

But again, it doesn't seem to work.

I tested it with a really slow service, and after 20 minutes it finally returned with the (correct) answer, but a timeout exception was not thrown.

Is it possible that the WCF service I am trying to reach is somehow blocking timeouts?


Solution

  • Answering my own question, almost a year later.

    I originally left this bug unresolved since it used to rarely happen. It resurfaced recently so I had to dig into it once again.

    As I found out, a TimeoutException wasn't thrown, simply because the SOAP request finished successfully. A timeout is only honoured from the moment the request goes out until a response returns. I verified that using fiddler. But still my code was hanging for hours.

    It appears the part blocking my code was simply the parsing of the SOAP response. The default WCF parser was simply hanging forever when trying to parse specific responses as XElement.

    I'm playing around with using different custom parsers and will post the results when I finish.