0
votes

I've got a WCF proxy that's using task-based operations. I'm making a method call that's wrapped in a Parallel.ForEach(). The service method I'm calling is a slow/long-running operation and occasionally returns exceptions. I'm not able to catch these exceptions in my catch block - never hits the block. The only way I'm able to catch exceptions is by using the synchronous service method. How do I catch the exceptions using the async service method? Is the exception being returned too late in the processing?

Parallel.ForEach(campaignResults.SelectMany(cd => cd), result =>
    {
        try
            {
                retList.TryAdd(client.SubmitRequestAsync(result.id, requestDetail), item.id);
            }
        catch (AggregateException ex)
            {
                // NEVER HIT ON EXCEPTION
                foreach (Exception e in ex.InnerExceptions)
                    {
                        Trace.TraceError(e.ToString());
                    }
            }
     }
1
FaultException<T> should be thrown, shouldn't? Did you try to catch common Exception? - Ilya Chumakov
@IlyaChumakov...Yes, I did try to catch Exception and doesn't work. - Big Daddy

1 Answers

2
votes

Maybe you should not be catching an AggregateException but rather a general exception and move the AggregateException handling outside the Parrallel.ForEach loop.

MSDN: Handle Exceptions in Parallel Loops

If you are using IIS to host your WCF service, just remember that there is a default 20 minute Idle Timeout unless changed. Background tasks do not count as non-idle requests and you might find IIS stops your WCF service because no further requests are made before the 20 minute Idle Timeout.