Bryan
Bryan

Reputation: 51

Catching Thrown Exception from Web Service in Silverlight App

I'm trying to throw and exception from my asmx web service and have the silverlight front end catch the exception within the completed event for the web service. Is this possible?

Upvotes: 2

Views: 1291

Answers (2)

John Saunders
John Saunders

Reputation: 161831

Web services don't have exceptions. They return SOAP Faults.

ASMX web services don't even support SOAP Faults properly.

Any uncaught exception will be turned into a SoapException. When an uncaught SoapException is thrown from the service, it will be returned as a SOAP Fault.

If you used "Add Web Reference" to create your proxy classes, then any SOAP Faults will be turned into a SoapException again.

On the other hand, WCF properly supports SOAP Faults both on the client and the service. A service operation can declare that it may return a particular kind of fault, say, "InvalidDataFault":

[OperationContract]
[FaultContract(typeof(InvalidDataFault))]
void SomeOperation(SomeDataContract request);

[DataContract]
public class InvalidDataFault
{
    [DataMember]
    public string Message {get;set;}

    [DataMember]
    public string PropertyName {get;set;}
}

The operation may then throw the fault:

throw new FaultException<InvalidDataFault>(
    new InvalidDataFault {Message="Some message", PropertyName="Property1"});

The client can then catch this exception and access the Details:

try
{
    proxy.SomeOperation(data);
}
catch (FaultException<InvalidDataFault> ex)
{
    // Can now process ex.Detail.Message and ex.Detail.PropertyName
}

Upvotes: 0

Vinay B R
Vinay B R

Reputation: 8421

There is no easy way doing this

you will have to wrap all your exceptions in the webservice as a fault exception
change status code to 200

check out this url for a sample

http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=silverlightws&DownloadId=3473

Upvotes: 3

Related Questions