Custom Exceptions with Web Services

C

Chris Dunaway

I'm creating a Web service and a Windows Forms application to consume it.
My question is about throwing a custom exception inside the WebService.
Can that be done, and can the custom web service be handled in the Windows
Forms app?

For example, suppose in my WebService, I have the following exception
class:

'\\\\\
<Serializable()> _
Public Class WebServiceException
Inherits ApplicationException <<<<<<< Is this correct?

'Code for exception class here

End Class
../////

And then within my web method:

'\\\\\
<WebMethod()> _
Public Sub SomeWebMethod()

If SomeCondition Then
Throw New WebServiceException
End If
End Sub
'/////


On the Windows Forms client, I can only trap for generic exceptions not my
custom exception type:

Try
WebService.SomeWebMethod
Catch ex As WebServiceException <<<<<<<<< This doesn't work
'Handle it
End Try


Anyone know of a way to achieve this?

Thanks for any pointers
 
S

Shiv Kumar

This seems to be a limitation of .NET SOAP implementation. Ideally things
should happen the way you're expecting (like they do in other SOAP
implementations), especially if both sides are built by you.

However, typically you should derive your SOAP exceptions from
System.Web.Services.Protocols.SoapException. This class has additional
properties you can set such as Actor, Code, Detail, Message when you throw
the exception at the server end.

Either ways, a SOAPException is raised on the client and you are left with
parsing the properties of this exception to figure out what really happened.
To go a step further, you could have a class that knows how to parse the
soap fault and raise the actual exception on the client provided, the client
is aware of that exception as well. Or maybe you could use SOAP Extensions
and do it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top