捕获 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;
}
}
}
以及一个用于测试它的基本应用程序(在单击事件上调用 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)
{
}
}
我想捕获我抛出的 SoapException
WebService,但我总是转到 FaultException
catch 块将其作为消息获取:
System.Web.Services.Protocols.SoapException:消息 在 [...]WebService1\WebService1\Service1.asmx.cs 中的 WebService1.Service1.Test(String str) 处:第 25 行
我怎样才能捕获真正的 SoapException,而不是FaultException ? 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):
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:
System.Web.Services.Protocols.SoapException: message
at WebService1.Service1.Test(String str) in [...]WebService1\WebService1\Service1.asmx.cs:line 25
How could I catch a real SoapException
, and not a FaultException
? Is there something I miss in the WebService?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为主要的“问题”是您正在使用连接到 ASP.NET Web 服务 (.asmx) 的 WCF 服务引用。
处理此问题的“最简单”方法可能是在客户端上使用 Web 引用而不是 WCF 服务引用。您可以通过选择“添加服务引用”对话框底部的“高级”按钮,然后选择该屏幕底部的“添加 Web 引用”来完成此操作。我相信使用 Web 引用应该会给您一个 SoapException。
正确的方法(如果您想遵循 Microsoft 的建议)是发布 WCF 服务而不是 .asmx 服务。不过那是另一章了..
I think that the main "problem" is that you are using a WCF Service Reference connecting to an ASP.NET Web Service (.asmx).
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.
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..
当 ASMX 服务引发
SoapException
时,.NET 将返回 SOAP 错误消息。SOAP 错误作为
FaultException
类型的异常返回到服务引用。所以你永远不会看到SoapException
。When an ASMX service throws a
SoapException
, .NET returns a SOAP Fault message.A SOAP Fault is returned to a service reference as an exception of type
FaultException
. So you will never see aSoapException
.