472,992 Members | 3,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Throwing Errors in Web Service

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
Aug 23 '06 #1
2 1869
>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?

SOAPFault is just exactly for the same.

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.org
http://www.edujini.in | http://webservices.edujini.in
-------------------
Aug 23 '06 #2
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
Aug 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
4
by: Eric Lilja | last post by:
Hello, in my program I have a function (pseudo code): void start_mysql_service() { obtain handle start mysql service using handle if start fails close handle and throw an exception...
12
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current...
1
by: René Meinecke | last post by:
hi there, i have created a webservice which is running well on the developer computer. for this i also created a windows installer setup which also works well on the developer computer, but if i...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
4
by: Claire | last post by:
I'm writing a realtime monitoring application acting as a Windows service. This service communicates to some instrumentation via a third party dll. TCP is the transport mechanism over the network....
3
by: Joel D. Kraft | last post by:
I've been logging a lot of fatal errors on my site lately, and I have not been able to figure out why. First, my site has error handling pages defined globally as shown below. They are not...
3
by: Baheri | last post by:
Can some some guide me to a good link on how to throw errors in a webservice? Also if there is a standard way of defining different degress of erros while exposing your webservice to thrid parties?...
21
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.