[英]How to catch a web service exception
如何从返回自定义对象的Web服务中捕获异常?
我看过这篇文章,但是它似乎没有显示如何获取服务抛出的异常。
我可以提取SOAP异常,但是我希望能够获取Web服务返回的原始异常。 我查看了此时设置的变量,并且似乎在任何地方都看不到异常,我只是看到:
"Server was unable to process request. ---> Exception of type
'RestoreCommon.ConsignmentNotFoundException' was thrown."
try
{
Consignment cons = WebServiceRequest.Instance.Service
.getConsignmentDetails(txtConsignmentNumber.Text);
lblReceiverName.Text = cons.Receiver.Name;
}
catch (ConsignmentNotFoundException)
{
MessageBox.Show("Consignment could not be found!");
}
这可能吗?
简而言之,没有。
Web服务将始终引发SOAP错误。 在您的代码中
编辑:如果您不想跨客户端发送异常,这可以做:
class BaseResponse
{
public bool HasErrors
{
get;
set;
}
public Collection<String> Errors
{
get;
set;
}
}
每个WebMethod响应都必须从此类继承。 现在,这是您的WebMethod块的外观:
public ConcreteResponse SomeWebMethod()
{
ConcreteResponse response = new ConcreteResponse();
try
{
// Processing here
}
catch (Exception exception)
{
// Log the actual exception details somewhere
// Replace the exception with user friendly message
response.HasErrors = true;
response.Errors = new Collection<string>();
response.Errors[0] = exception.Message;
}
finally
{
// Clean ups here
}
return response;
}
这只是一个例子。 您可能需要编写适当的异常处理代码,而不是简单地使用通用catch块。
注意:这将只处理应用程序中发生的异常。 客户端与服务之间的通信过程中发生的任何异常仍将抛出给客户端应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.