Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am writing a SOAP based ASP.NET Web Service having a number of methods to deal with Client objects. e.g:

  • int AddClient(Client c) => returns Client ID when successful
  • List GetClients()
  • Client GetClientInfo(int clientId) In the above methods, the return value/object for each method corresponds to the "all good" scenario i.e. A client Id will be returned if AddClient was successful or a List<> of Client objects will be returned by GetClients. But what if an error occurs, how do I convey the error message to the caller?

I was thinking of having a Response class: Response { StatusCode, StatusMessage, Details } where Details will hold the actual response but in that case the caller will have to cast the response every time. What are your views on the above? Is there a better solution?

---------- UPDATED -----------
Is there something new in WCF for the above? What difference will it make If I change the ASP.NET Web Service to a WCF Service?

share|improve this question
2  
You would return an HTTP error code, like 500. –  user281377 Mar 8 '11 at 16:19
    
@ammoQ: Please post your answer as an answer so we can upvote it. –  S.Lott Mar 8 '11 at 21:00
    
Are you aware that XML SOAP Response documents are the norm? Your approach already is the standard practice. Why are you asking? –  S.Lott Mar 8 '11 at 21:03
    
S.Lott: OK, done. I thought it was too short and too obvious, though. –  user281377 Mar 9 '11 at 0:48
    
For .net specific aspects of the soap protocol, you may want to review the framework documentation, and I would start here:msdn.microsoft.com/en-us/library/ds492xtk(v=VS.100).aspx Make sure it refers to the specific version of the System.Web.Services reference you are using. –  JustinC Mar 10 '11 at 2:39
show 1 more comment

5 Answers

If you're looking for an equivalent of exception, the proper way is to use SOAP fault messages.

.Net will also convert uncaught exceptions to fault messages - so this should be transparent

Exception Handling

Any exception thrown in a .NET Web service that is not of type SoapException is automatically wrapped in a SoapException by the .NET framework. In turn, the SoapException will then be converted to a SOAP fault message that is returned to the caller of the Web service. Because the Basic Profile has restrictions on SOAP fault messages, there are restrictions on how you throw a SoapException so that, when the .NET framework generates the SOAP Fault message, it does not violate any rules of the Basic Profile. These restrictions concern both the Code property and the Detail property.

from How to Apply the Basic Profile

share|improve this answer
add comment

You want to return standard var types (int, string). Create a class for your code to use, but actually return the fields as string, for example...

[DataMember]
int ID

[DataMember]
string StatusCode

[DataMember]
string StatusMessage

[DataMember]
string Details
share|improve this answer
add comment

What are your views on the above?

Read this: http://www.w3schools.com/SOAP/soap_example.asp

Your solution is standard

Is there a better solution?

No. It's the standard approach.

share|improve this answer
add comment

You would return an HTTP error code, like 500.

share|improve this answer
add comment

We usually use detailed activity log files and error log files to keep track of what is going on at any given time. This works well for web and desktop services.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.