捕获ASP.NET WebService上WebMethod引发的自定义异常

Mus*_*lır 8 c# asp.net web-services exception

我有一个经典的asp.net Web服务(asmx)和一个web方法.我需要在我的web方法中为某些情况抛出一个自定义异常,我需要捕获特定的自定义异常,我调用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 HelloWorldException("Hello World Exception", ex);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输入,输出和异常类作为示例:

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) { }
}
Run Code Online (Sandbox Code Playgroud)

在客户端,我需要:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不能这样做,因为当我在客户端添加Web服务引用时,我有服务类,输入和输出类,但我没有自定义异常类.

还有一个问题是,我也有序列化Exception类的问题(因为Exception.Data属性实现了IDictionary接口)

有没有办法以我的方式做到这一点,或者我是一个完全错误的方式,还是有什么我想念的Web服务的基础?

谢谢.

Mus*_*lır 11

ASP.NET Web Service可以抛出任何类型的异常:SoapException,HelloWorldException或者soever.但是,异常被序列化为SOAP Fault元素,并且无论服务中抛出的异常类型如何,异步都会在反序列化时转换为SoapException.因此,即使我将HelloWorldException部署到客户端,也不可能在我的问题中使用我的catch块捕获HelloWorldException.

对于ASP.NET客户端,唯一的方法是将异常捕获为SoapException并通过其ActorSoapFaultSubCode属性处理它.

我基本上解决了我的问题如下:

网络服务:

[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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

连同Bryce Fischer在回答中写的文件; 这些msdn文档还提供有关抛出和处理Web服务异常的有用信息.

如何:从使用ASP.NET创建的Web服务中抛出异常

如何:处理Web服务方法引发的异常