FaultException.Detail返回为空 [英] FaultException.Detail coming back empty

查看:210
本文介绍了FaultException.Detail返回为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WCF客户端上捕获给定的FaultException。我基本上需要从故障类中提取一个内部描述,以便可以将其打包到另一个异常中,以使上层可以执行任何操作。

I am trying to catch a given FaultException on a WCF client. I basically need to extract a inner description from the fault class so that I can then package it in another exception for the upper layers to do whatever.

我已经成功完成了很多次,这次与之不同的是,将故障声明为数组,如您从服务引用属性中所见在引发异常的方法顶部声明:

I've done this successfully a number of time, what makes it different this time is that fault is declared as an array, as you can see from the service reference attribute declared on top of the method that throws the exception:

[System.ServiceModel.FaultContractAttribute(typeof(FaultClass[]), Action = "http://whatever/", Name = "whateverBusinessFault")] 

这是我的代码:

try
{
  // call service here
}
catch (FaultException<FaultClass[]> ex)
{
  if (ex.Detail != null && ex.Detail.Length > 0)
  {
    throw new CustomException(ex.Detail[0].description);
  }
  else
  {
    throw;
  }
}

问题是 Detail(这是一个数组) )总是在代码中变空,即使我可以从WCF跟踪的SOAP响应中看到数据(描述字段等)。

Problem is Detail (which is an array) is always coming back empty in the code even if I can see the data (description field etc.) in the SOAP response from WCF trace.

因此,我需要的东西肯定会回来,但由于某种原因,要么没有被反序列化,要么我无法从代码中获取。

So the stuff I need is definitely coming back but for some reason either it doesn't get deserialized or I can't get to it from code.

任何帮助表示赞赏!

更新

尝试使用@Darin建议但没有运气,我从XmlReader中提取的字符串是 / r / n:

Trying with @Darin suggestion but no luck, the string I am extracting from the XmlReader is "/r/n":

var sb = new StringBuilder();

using (XmlReader reader = fault.GetReaderAtDetailContents())
{
  while (reader.Read())
     sb.AppendLine(reader.ReadOuterXml()); 
}

var detail = sb.ToString();

详细信息部分根本没有出现!

Looks like the detail section is not coming up at all!

推荐答案

我在UPS论坛上找到了解决方案:

I found the solution on a UPS Forum :

https://developerkitcommunity.ups.com/index.php/Special:AWCforum/st/id371

问题是可视工作室没有正确地映射ErrorDetail对象。ErrorDetail节点称为 ErrorDetail,但为其生成的类型为 ErrorDetailType。 我编辑了为我正在使用的每个服务生成的reference.cs类,并添加了TypeName:

"The problem was the visual studio didn't quite map out the ErrorDetail objects right. The ErrorDetail node is called "ErrorDetail", but the type generated for it is "ErrorDetailType." I edited the reference.cs class generated for each service I was using and added a TypeName:"

这篇关于FaultException.Detail返回为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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