我有一個使用C#Web服務的Winforms應用程序。 如果WebService引發異常,我的客戶端應用程序總是得到一個SoapException而不是「真正的」異常。Web服務異常處理
這裏有一個演示:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
throw new IndexOutOfRangeException("just a demo exception");
}
}
現在,在客戶端,我希望能夠以不同的方式來處理不同的異常。
try
{
ServiceReference1.Service1SoapClient client
= new ServiceReference1.Service1SoapClient();
Button1.Text = client.HelloWorld();
}
catch (IndexOutOfRangeException ex)
{
// I know how to handle IndexOutOfRangeException
// but this block is never reached
}
catch (MyOwnException ex)
{
// I know how to handle MyOwnException
// but this block is never reached
}
catch (System.ServiceModel.FaultException ex)
{
// I always end in this block
}
但是,這並不工作,因爲我總是得到「System.ServiceModel.FaultException」我只能通過解析異常的消息財產找出「真正的」異常:
System.Web.Services.Protocols.SoapException: Server was unable
to process request. ---> System.IndexOutOfRangeException: just a demo\n
at SoapExceptionTest.Service1.Service1.HelloWorld() in ...
--- End of inner exception stack trace ---
是有辦法讓這項工作以某種方式?