通过捕获一个WebService抛出的SoapException [英] Catching a SoapException thrown by a WebService

查看:249
本文介绍了通过捕获一个WebService抛出的SoapException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的服务:

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):

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

    }
}

我想搭上的SoapException 我的WebService的抛出,但我总去在的FaultException catch块一个得到的消息:

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:

System.Web.Services .Protocols.SoapException:消息
在WebService1.Service1.Test(字符串str)在[...] WebService1\WebService1\Service1.asmx.cs:第25行

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

我怎么能抓到真正的的SoapException ,而不是的FaultException ?是不是我的WebService的错过?

How could I catch a real SoapException, and not a FaultException? Is there something I miss in the WebService?

推荐答案

我认为主要的问题是,你正在使用WCF服务参考连接到ASP.NET Web服务(的.asmx)。

I think that the main "problem" is that you are using a WCF Service Reference connecting to an ASP.NET Web Service (.asmx).

在最简单的方法来处理这很可能是使用Web引用,而不是客户端上的WCF服务引用。您通过选择添加服务引用对话框的底部,然后在该屏幕底部的添加Web引用高级按钮,做到这一点。我相信,使用Web引用应该给你的SoapException。

The "easiest" way to handle this would probably be to use a Web Reference instead of a WCF Service Reference on the client. 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. I believe that using a Web Reference should give you a SoapException.

正确的方法(如果要遵循微软的建议)将发布一个WCF服务,而不是一个的.asmx服务。这是一个整体的其他章节虽然..

The correct way (if you want to follow Microsofts advice) would be to publish a WCF service instead of an .asmx service. That is a whole other chapter though..

这篇关于通过捕获一个WebService抛出的SoapException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆