What I do is trap the exception within the Web Service and then throw
and then throw it as a SoapException.
In Web Service:
Try
' test for some condition and treat it like a system
exception
If Something_Bad_Happened() Then
Throw New System.Exception("Something Bad Happened
Exception")
End If
Catch ex As System.Exception
' certain types of exceptions you might re-throw as a
SoapExceptions
' other types you might ignore or handle on the web service
side.
If ex.Message = "Something Bad Happened Exception" Then
' Build the detail element of the SOAP fault.
Dim doc As New System.Xml.XmlDocument
Dim node As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
SoapException.DetailElementName.Name, _
SoapException.DetailElementName.Namespace)
' Build specific details for the SoapException.
' Add details as the first child to the XML element
that is already created.
Dim details As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo1", "http://tempuri.org/")
' Add details2 as the first child to the XML element
that is already created.
Dim details2 As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo2", "http://tempuri.org/")
Dim attr As XmlAttribute = doc.CreateAttribute("t", _
"attrName", "http://tempuri.org/")
attr.Value = "attrValue"
details2.Attributes.Append(attr)
' Append the two child elements to the detail node.
node.AppendChild(details)
node.AppendChild(details2)
'Throw SOAP exception to be returned by Web Service
Dim se As New SoapException("The Web Service Failed
Because Something Really Bad Happened.", _
SoapException.ServerFaultCode, _
HttpContext.Current.Request.Url.AbsoluteUri, node)
Throw se
Else
Throw ex
End If
End Try
On Client Side:
Try
WS.MyWebServiceMethodCall()
Catch Se As System.Web.Services.Protocols.SoapException
MsgBox(Se.Message,
MsgBoxStyle.Critical, "Soap Exception in
thrown in Web Service")
Catch ex As System.Exception
MsgBox(ex.Message, _
MsgBoxStyle.Critical, "Exception in
Client")
End Try
This way you can distinguish between exceptions caused by the Web
Service and exceptions thrown in the client.
The error thrown by the Web Service may have originally been an
exception or just some error condition that you decide to throw as a
SoapException.
You don't really need to worry about the Web Service parameters since
once the Soap Exception is thrown they are no longer relevant.
If the exception is originally thrown by the web service, with a little
more work, you can throw it as a SoapException with the inner exception
containing the original message string.
I'm not sure if this is the best practice, but if you hunt around on
msdn.com I think there are some other examples of how to use
SoapException. I just threw this together so it's a bit sloppy, but
you get the idea...
Good Luck,
-Paul
Anthony Biondo Jr wrote:
I was wondering how to handle an error in a web service. If our web service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error attached
or is there some other way to do this?
thanks,
Anthony