简体   繁体   English

捕获WebService抛出的SoapException

[英]Catching a SoapException thrown by a WebService

I wrote the following service : 我写了以下服务:

namespace WebService1
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string Test(string str)
        {
            if (string.IsNullOrEmpty(str))
                throw new SoapException("message", SoapException.ClientFaultCode);
            else
                return str;
        }
    }
}

And a basic application to test it (one button calling the Test method on click event): 并且有一个基本的应用程序来测试它(一个按钮在click事件上调用Test方法):

private void button1_Click(object sender, EventArgs e)
{
    ServiceReference1.Service1SoapClient ws = new WindowsFormsApplication1.ServiceReference1.Service1SoapClient();

    try
    {
        ws.Test("");
    }
    catch (SoapException ex)
    {
         //I never go here
    }
    catch (FaultException ex)
    {
        //always go there
    }
    catch (Exception ex)
    {

    }
}

I'd like to catch the SoapException thrown by my WebService, but I always go to the FaultException catch block a get that as message: 我想捕获我的WebService抛出的SoapException ,但我总是转到FaultException catch块获取该消息:

System.Web.Services.Protocols.SoapException: message at WebService1.Service1.Test(String str) in [...]WebService1\\WebService1\\Service1.asmx.cs:line 25 System.Web.Services.Protocols.SoapException:[...] WebService1 \\ WebService1 \\ Service1.asmx.cs中的WebService1.Service1.Test(String str)的消息:第25行

How could I catch a real SoapException , and not a FaultException ? 我怎么能捕获真正的SoapException ,而不是FaultException Is there something I miss in the WebService? WebService中有什么我想念的吗?

I think that the main "problem" is that you are using a WCF Service Reference connecting to an ASP.NET Web Service (.asmx). 我认为主要的“问题”是您正在使用连接到ASP.NET Web服务(.asmx)的WCF服务引用。

The "easiest" way to handle this would probably be to use a Web Reference instead of a WCF Service Reference on the client. 处理此问题的“最简单”方法可能是在客户端上使用Web引用而不是WCF服务引用。 You do this by selecting the "Advanced" button on the bottom of the Add Service Reference dialog, and then the Add Web Reference on the bottom of that screen. 您可以通过选择“添加服务引用”对话框底部的“高级”按钮,然后选择该屏幕底部的“添加Web引用”来执行此操作。 I believe that using a Web Reference should give you a SoapException. 我相信使用Web Reference应该会给你一个SoapException。

The correct way (if you want to follow Microsofts advice) would be to publish a WCF service instead of an .asmx service. 正确的方法(如果你想遵循微软的建议)将是发布WCF服务而不是.asmx服务。 That is a whole other chapter though.. 这是另一章,但..

When an ASMX service throws a SoapException , .NET returns a SOAP Fault message. 当ASMX服务抛出SoapException ,.NET将返回SOAP Fault消息。

A SOAP Fault is returned to a service reference as an exception of type FaultException . SOAP Fault作为FaultException类型的异常返回给服务引用。 So you will never see a SoapException . 所以你永远不会看到SoapException

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

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