如何在Task故障中访问SOAP异常 [英] How to access SOAP exception in a Task fault

查看:63
本文介绍了如何在Task故障中访问SOAP异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个客户端应用程序,可以使用一些较旧的服务.客户端应用程序将异步调用多个服务方法.有时,服务会返回SOAP异常.客户端应用程序需要访问SOAP异常.可以吗 异步执行多个任务时完成了什么?

We have a client application which hits some older services. The client application will call multiple service methods asynchronously. Sometimes the service returns a SOAP exception. The client application needs access to the SOAP exception. Can this be done when executing multiple Tasks asynchronously?

下面,客户端需要Console.Write所在的SOAP异常

Below, the client needs the SOAP exception where the Console.Write is

var allTasks = new List<Task>();

var t1 = client.methodOneAsync(eaRequest1);
allTasks.Add(t1);

var t2 = client.methodTwoAsync(eaRequest2);
allTasks.Add(t2);

var t3 = client.methodThreeAsync(eaRequest3);
allTasks.Add(t3);

try
{
    Task executeTasks = Task.WhenAll(allTasks);
    executeTasks.Wait();
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

更新:

服务的WSDL具有Fault异常模式,该模式扩展了基本的SOAP故障.扩展故障包含一系列消息.生成的引用类具有这些对象.但是,发生故障时,不会填充它们.故障确实 拥有类名[]的详细信息,但是,该数组为空.

The service's WSDL has a Fault exception schema which is extending a basic SOAP fault. The extended fault has an array of messages. The generated reference classes have these objects. However, when the fault occurs, they are not populated. The fault does have a Detail of the classname[], however, the array is empty.

推荐答案

任务自动在 Task.Exception属性.由于您可能会有继续的机会(一个任务失败,但是无论如何另一个任务都会继续),因此该属性具有一个"AgregateException". -基本上是已发生的所有未处理异常的列表.

Tasks automatically catch an expose exceptions for you in the Task.Exception Property. Due the the chance that you might have continuations (one task fails, but another continues anyway), that property has a "AgregateException" - basically a list of all unhandeled exceptions that have happened.

只需检查并重新抛出.

Just check for it and rethrow.

我的一个小小的宠物怒气冲冲地看到任何捕获物(异常例外).虽然公开它们是好的,但是您仍在阻止应用程序关闭.您确实不希望您的应用程序在发生致命异常后仍能运行.接下来会做什么 完全不可预测.
烦恼异常-编码中的奇妙冒险-站点首页-MSDN博客

A minor pet peeve of mine is seeing any catch(Exception ex) blocks. While exposing them is good, you are still preventing the application from closing. You really do not want your application to still run after a fatal exception. What it does next would be entirely unpredictable.
Vexing exceptions - Fabulous Adventures In Coding - Site Home - MSDN Blogs

.NET中的异常处理最佳实践-CodeProject


这篇关于如何在Task故障中访问SOAP异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆