You can't do this:
- Web services do SOAP Faults. Exceptions are platform-specific.
- When an exception is unhandled in an ASMX web service, .NET will translate it into a SOAP Fault. The details of the exception are not serialized.
- In an ASMX client, a SOAP fault will be translated into a SoapException.
ASMX web services do not have proper support for SOAP Faults. There is no way to get any exception other than a SoapException on the client side.
Yet another reason to upgrade to WCF.
As an example of what you can't do with ASMX, here's how WCF works. WCF allows you to specify, for each web service operation, which faults it can return:
[ServiceContract]
public interface IMyServiceContract
{
[FaultContract(typeof(IntegerZeroFault))]
[FaultContract(typeof(SomeOtherFault))]
[OperationContract]
public string GetSomeString(int someInteger);
}
[DataContract]
public class IntegerZeroFault
{
[DataMember]
public string WhichInteger {get;set;}
}
[DataContract]
public class SomeOtherFault
{
[DataMember]
public string ErrorMessage {get;set;}
}
public class MyService : IMyServiceContract
{
public string GetSomeString(int someInteger)
{
if (someInteger == 0)
throw new FaultException<IntegerZeroFault>(
new IntegerZeroFault{WhichInteger="someInteger"});
if (someInteger != 42)
throw new FaultException<SomeOtherFault>(
new SomeOtherFault{ErrorMessage ="That's not the anaswer"});
return "Don't panic";
}
}
A WCF client can then catch FaultException<SomeOtherFault>
, for instance. When I've tried this with a Java client, it was able to catch SomeOtherFault
, which IBM Rational Web Developer created to derive from the Java Exception
class.