简体   繁体   English

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

[英]Catching a custom Exception thrown by a WebMethod on ASP.NET WebService

I have a classical asp.net web service (asmx) and a web method in it. 我有一个经典的asp.net Web服务(asmx)和一个web方法。 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. 我需要在我的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);
        }
    }
}

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) { }
}

In the client side, I need: 在客户端,我需要:

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
    }
}

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

Also another problem is that, I have also problems with serializing Exception class (because of Exception.Data property implements IDictionary interface) 还有一个问题是,我也有序列化Exception类的问题(因为Exception.Data属性实现了IDictionary接口)

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

Thanks. 谢谢。

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

For an ASP.NET client, the only way is to catch the exception as SoapException and handle it by its Actor or SoapFaultSubCode property. 对于ASP.NET客户端,唯一的方法是将异常捕获为SoapException并通过其ActorSoapFaultSubCode属性处理它。

I've basically solved my problem as below: 我基本上解决了我的问题如下:

Web Service: 网络服务:

[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);
        }
    }
}

Client: 客户:

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
    }
}

Together with the document which Bryce Fischer wrote in his answer; 连同Bryce Fischer在回答中写的文件; these msdn documents also has helpful information about throwing and handling web service exceptions. 这些msdn文档还提供有关抛出和处理Web服务异常的有用信息。

How to: Throw Exceptions from a Web Service Created Using ASP.NET 如何:从使用ASP.NET创建的Web服务中抛出异常

How to: Handle Exceptions Thrown by a Web Service Method 如何:处理Web服务方法引发的异常

This may be of help. 可能有所帮助。 Looks like you'll get a SoapException, but you can inspect the details to determine if its your class or not. 看起来你会得到SoapException,但是你可以检查细节以确定它是否属于你的类。

Side note, to have access to "HelloWorldException", you can pull it out into a seperate assembly and deploy that on the client... 注意,要访问“HelloWorldException”,您可以将其拉出一个单独的程序集并在客户端上部署它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM