如何禁用soap web服务的详细错误消息?
我有一个带有一些段落的网络方法。
示例请求:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GenerateTestRequest xmlns="http://tempuri.org/">
<sId>1@!aB</sId>
<mobileNum>12345678</mobileNum>
<srcType>1</srcType>
</GenerateTestRequest>
</Body>
</Envelope>
当意外的输入被提供给 sId
para 而不是 Int32 时,Web 服务在响应内容中返回了一个异常。
示例资源:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
<faultstring xml:lang="en-SG">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:sId. The InnerException message was 'There was an error deserializing the object of type System.Int32. The value '1@!aB' cannot be parsed as the type 'Int32'.'. Please see InnerException for more details.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
我不想显示异常消息的响应内容。有没有办法关闭堆栈跟踪或异常详细信息?
答案 0 :(得分:1)
有没有办法关闭堆栈跟踪或异常详细信息?
是的,通过对 SOAP 网络服务进行适当的错误处理。
SOAP 请求可以收到成功的 SOAP 响应,也可以以 SOAP 故障的形式收到不成功的响应。不幸的是,人们只关注成功案例,而让他们使用的框架处理其他问题。
因为 SOAP 是一种协议,服务器上的任何异常都需要转换为正确的故障响应。这需要由开发人员通过捕获任何异常并从中构建适当的故障响应来完成,其中只有所需的数据。出于安全原因,数据当然不能包含堆栈跟踪或其他敏感信息。但是就像我说的,人们经常忽略处理错误情况,并且框架无法构建自定义错误,它只能构建一个 SOAP 错误,其中包含发生的异常的详细信息,并将其暴露给客户端。
您没有标记此 WCF
,但错误代码上的架构命名空间显示为 windowscommunicationfoundation
,因此我假设这是 Web 服务框架。您可以通过将 IncludeExceptionDetailInFaults
设置为 false(我认为您的服务现在设置为 true)来替换所有异常消息。请参阅此处了解详情:either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server
或者,您可以进行适当的错误处理并定义和记录客户端可以接收的可能故障并将任何异常转换为这些故障之一。您可以从这里开始阅读:Specifying and Handling Faults in Contracts and Services
如果您使用任何其他 Web 服务框架或库,想法是相同的,要阅读的文档会有所不同。