捕获由ASP.NET WebService上的WebMethod引发的自定义异常 [英] Catching a custom Exception thrown by a WebMethod on ASP.NET WebService
问题描述
我有一个经典的asp.net Web服务(asmx)和其中的一个web方法.在某些情况下,我需要在Web方法中引发一个自定义异常,并且需要在调用Web服务方法的地方捕获该特定的自定义异常.
I have a classical asp.net web service (asmx) and a web method in it. I need to throw a custom exception for some case in my web method, and I need to catch that specific custom exception where I call the web service method.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public HelloWorldOutput HelloWorld(HelloWorldInput input)
{
try
{
// My Code
return new HelloWorldOutput();
}
catch (Exception ex)
{
throw new HelloWorldException("Hello World Exception", ex);
}
}
}
输入,输出和异常类作为示例:
Input, output and exception classes as a sample:
public class HelloWorldInput { }
public class HelloWorldOutput { }
[Serializable]
public class HelloWorldException : Exception
{
public HelloWorldException() { }
public HelloWorldException(string message) : base(message) { }
public HelloWorldException(string message, Exception inner)
: base(message, inner) { }
protected HelloWorldException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
在客户端,我需要:
public static void Main()
{
WebService service = new WebService();
try
{
service.HelloWorld(new HelloWorldInput());
}
catch (HelloWorldException ex)
{
// Do sth with HelloWorldException
}
catch (Exception ex)
{
// Do sth with Exception
}
}
但是,我不能这样做,因为在客户端上添加Web服务引用时,我具有服务类,输入和输出类,但是没有自定义异常类.
However, I cannot do that because when I add the web service reference on the client, I have service class, input and output classes, but I do not have custom exception class.
另一个问题是,我也有序列化Exception类的问题(由于Exception.Data属性实现了IDictionary接口)
Also another problem is that, I have also problems with serializing Exception class (because of Exception.Data property implements IDictionary interface)
是否有办法做到这一点,或者我是完全错误的方式,还是我错过了Web服务基础知识?
Is there a way to do this in my way, or am I in a completely wrong way, or is there something I miss about fundamentals of web services?
谢谢.
推荐答案
ASP.NET Web服务可以引发任何类型的异常:SoapException,HelloWorldException或类似的异常.但是,该异常被序列化为SOAP Fault元素,并且无论服务中引发的异常的类型如何,在反序列化时,该异常都将转换为SoapException.因此,即使我将HelloWorldException部署到客户端,也无法用问题中的catch块捕获HelloWorldException.
ASP.NET Web Service can throw any type of exception: SoapException, HelloWorldException or soever. However, the exception is serialized into a SOAP Fault element, and regardless of the type of the exception thrown in the service, the exception is converted into a SoapException while deserialization. Hence, it is not possible to catch the HelloWorldException with my catch block in my question, even if I deploy the HelloWorldException to the client.
对于ASP.NET客户端,唯一的方法是将异常捕获为SoapException并通过其 Actor
Actor 或 SoapFaultSubCode
属性对其进行处理.
For an ASP.NET client, the only way is to catch the exception as SoapException and handle it by its Actor
or SoapFaultSubCode
property.
我基本上已经解决了如下问题:
I've basically solved my problem as below:
Web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public HelloWorldOutput HelloWorld(HelloWorldInput input)
{
try
{
// My Code
return new HelloWorldOutput();
}
catch (Exception ex)
{
throw new SoapException("Hello World Exception",
SoapException.ServerFaultCode, "HelloWorld", ex);
}
}
}
客户:
public static void Main()
{
WebService service = new WebService();
try
{
service.HelloWorld(new HelloWorldInput());
}
catch (SoapException ex)
{
if(ex.Actor == "HelloWorld")
// Do sth with HelloWorldException
}
catch (Exception ex)
{
// Do sth with Exception
}
}
连同布莱斯·菲舍尔(Bryce Fischer)在回答中写的文件;这些msdn文档还具有有关引发和处理Web服务异常的有用信息.
Together with the document which Bryce Fischer wrote in his answer; these msdn documents also has helpful information about throwing and handling web service exceptions.
这篇关于捕获由ASP.NET WebService上的WebMethod引发的自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!