【发布时间】:2011-05-16 12:52:18
【问题描述】:
我有一个简单的 WCF HTTP/SOAP Web 服务,服务实现看起来像这样:
public CustomResponse DoSomething(CustomRequest request)
{
try
{
return InternalGubbins.WithErrorHandling.ProcessRequest(request);
}
catch
{
// Some sort of error occurred that is not gracefully
// handled elsewhere in the framework
throw new SoapException("Hmmm, it would seem that the cogs are meshed!", SoapException.ServerFaultCode);
}
}
现在,如果抛出 SoapException,我希望将异常消息(即Hmmm, it would seem that the cogs are meshed!)返回给调用客户端,而不需要任何额外的异常细节(即堆栈跟踪)。
如果我将 includeExceptionDetailInFaults 设置为 true(服务器上的 web.config),那么带有堆栈跟踪等的完整异常将返回给客户端。但是,如果我将其设置为 false,我会收到一条通用消息:
服务器无法处理 由于内部错误而请求。为了 有关错误的更多信息, 要么打开 IncludeExceptionDetailInFaults(要么 来自 ServiceBehaviorAttribute 或来自 配置 行为)在服务器上,以便 将异常信息发回给 客户端,或根据 Microsoft .NET Framework 3.0 SDK 文档并检查服务器 跟踪日志。
所以问题是,我怎样才能将我的 SoapException 消息返回给调用客户端?即:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
<a:RelatesTo>urn:uuid:185719f4-6113-4126-b956-7290be375342</a:RelatesTo>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Receiver</s:Value>
<s:Subcode>
<s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-GB">Hmmm, it would seem that the cogs are meshed!</s:Text>
</s:Reason>
</s:Fault>
</s:Body>
</s:Envelope>
【问题讨论】:
标签: wcf custom-errors soapexception