1

我试图查看我的 WCF 服务中引发的异常,但我从响应中得到的只是:

“由于内部错误,服务器无法处理请求。有关错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为),以便将异常信息发送回客户端,或根据 Microsoft .NET Framework 3.0 SDK 文档打开跟踪并检查服务器跟踪日志。”

由于我以“REST”方式进行操作,因此我的 web.config 中没有这些选项。那么,如何在使用 WCF 4 REST 模板时启用“IncludeExceptionDetailInFaults”?

4

1 回答 1

0

这是我想出的:

在 web.config 中的 standardEndpoint 下,打开 faultExceptionEnabled

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true">

然后,要显示自定义消息,抛出的异常必须是 FaultException。这是我使用的一个示例:

if (!Enum.GetNames(typeof(Models.Games.GsfCurrencyPrice.Sections)).Contains(section)) throw new FaultException<ArgumentException>(new ArgumentException("Value must be one of the following: " + string.Join(", ", Enum.GetNames(typeof(Models.Games.GsfCurrencyPrice.Sections))), "section"));

抛出时会产生以下响应:

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code><Value>Sender</Value></Code><Reason><Text xml:lang="en-US">The creator of this fault did not specify a Reason.</Text></Reason><Detail><ArgumentException xmlns="http://schemas.datacontract.org/2004/07/System" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema"><ClassName i:type="x:string" xmlns="">System.ArgumentException</ClassName><Message i:type="x:string" xmlns="">Value must be one of the following: Buy, Sell</Message><Data i:nil="true" xmlns=""/><InnerException i:nil="true" xmlns=""/><HelpURL i:nil="true" xmlns=""/><StackTraceString i:nil="true" xmlns=""/><RemoteStackTraceString i:nil="true" xmlns=""/><RemoteStackIndex i:type="x:int" xmlns="">0</RemoteStackIndex><ExceptionMethod i:nil="true" xmlns=""/><HResult i:type="x:int" xmlns="">-2147024809</HResult><Source i:nil="true" xmlns=""/><WatsonBuckets i:nil="true" xmlns=""/><ParamName i:type="x:string" xmlns="">section</ParamName></ArgumentException></Detail></Fault>

希望这可以帮助。

于 2011-02-04T01:31:57.327 回答