WCF HTTP/SOAP Web 服务 - 在错误中返回异常消息
我有一个简单的 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,我想要异常消息(即 嗯,看起来齿轮是啮合的!
)返回到调用客户端,没有任何额外的异常详细信息(即堆栈跟踪)。
如果我将 includeExceptionDetailInFaults
设置为 true(服务器上的 web.config),则带有堆栈跟踪等的完整异常将返回给客户端。但是,如果我将其设置为 false,我会收到一条通用消息:
服务器无法处理 由于内部错误而请求。为了 有关错误的更多信息, 要么打开 IncludeExceptionDetailInFaults(或者 来自 ServiceBehaviorAttribute 或来自 配置 行为)在服务器上,以便 将异常信息发送回 客户端,或按照以下方式打开跟踪 Microsoft .NET Framework 3.0 SDK 记录并检查服务器 跟踪日志。
所以问题是,如何将 SoapException 消息返回给调用客户端? IE:
<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>
I have asimple WCF HTTP/SOAP web service, the service implementation looks something like this:
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);
}
}
Now, if that SoapException is thrown I would like the exception message (i.e. Hmmm, it would seem that the cogs are meshed!
) to be returned to the invoking client without any additional exception detail (i.e. stack traces).
If I set includeExceptionDetailInFaults
to true (web.config on the server) then the full exception, with stack traces etc, is return to the client. However if I set it to false, I get a generic message:
The server was unable to process the
request due to an internal error. For
more information about the error,
either turn on
IncludeExceptionDetailInFaults (either
from ServiceBehaviorAttribute or from
the configuration
behavior) on the server in order to
send the exception information back to
the client, or turn on tracing as per
the Microsoft .NET Framework 3.0 SDK
documentation and inspect the server
trace logs.
So the question is, how can I get my SoapException message back to the invoking client? i.e:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在操作上声明FaultContract并使用FaultException(SoapException是WCF之前的)。我相信如果故障不是服务合同的一部分,WCF 不会将故障发送回客户端。我从未尝试过 SoapException,但抛出FaultException 对我来说总是很有效。
I think you need to declare a FaultContract on the operation and use FaultException (SoapException is pre WCF). I believe WCF does not send faults back to the client if they are not part of the service contract. I have never tried SoapException but certainly throwing a FaultException has always worked fine for me.
如果您不想定义自定义异常类型,请尝试这样
做,这样做会将以下响应发送给客户端
If you don't want to define a custom exception type then try this
Doing so would send the following response to the client