简体   繁体   中英

WCF Task-Based Operations Exception Handling and TPL

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());
                    }
            }
     }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM