Catching a WCF Timeout Exception

Paul Michaels

I have a WCF service that invokes a potentially long running DB procedure. In the event that this causes a timeout in WCF, I want to handle this gracefully in the client.

Here is the code that I'm using the call the WCF service:

async Task<int> RunWCFServiceCall()
{
    try
    {
        int returnVal = await Task.Run<int>(() => CallService());

        return returnVal;
    }
    catch (FaultException ex)
    {
        if (ex.InnerException.GetType().Equals(typeof(TimeoutException)))
        {
            return 0;
        }

The code above catches the exception as a FaultException, but fails the inner exception check. The reason being that InnerException is null; however, the exception message is:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

My question is: how can I trap this error, specifically, without resorting to a messy construct of checking the exception message string?

Richard

By default, WCF will be hiding the actual exception details, in much the same way that a production webserver won't display an error message. You need to control the way the error is raised.

You can trap the exception in your service and raise it yourself as a FaultException with a specific FaultReason or FaultCode, or you can include the exception in your WCF contract.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related