6

I'm now toying around with WebServices, using the .NET framework (.asmx files, not WCF). I'm wondering what's the best practice to tell the user that some sort of business-error has happened in the method call.

My small test-case:

I have measuring probes which need to register with a central server. Each probe should have a different physical address. To register, they should call (via a web service):

[WebMethod]
public void RegisterReadingStation(out Guid sessionId, Int64 physicalAddress)

Now, the signature is not set in stone - actually, that's what I'm trying to figure out. :) I need to somehow alert the probe if it's trying to register itself using a physical address that's already taken.

The way I see it, I got a few possibilities:

  • Throw a SoapException containing the information. However, a string::message isn't really that easy to do programmatic checks on.
  • Use some sort of value-class as a return parameter (or even simpler, an enum). If I do this, I guess I had to manually serialize / deserialize the class on the server/client?

Any thoughts about this?

2 Answers 2

5

I return a small class called ResultSet from each WebMethod, which contains an int errorcode and a string errormessage.

This gives you an easy check for error/success, and some details if things go wrong.

If the WebMethod needs to return data, I'll inherit from ResultSet to give a specific ResultSet to include the data as well.

3
  • Yes, that was were I was heading as well :)
    – cwap
    Mar 26, 2009 at 18:09
  • But what to do when the web service function is supposed to return an object like employeeData when all goes well and should return a error message when there is error.How to return two different objects from same function in different scenario ? Apr 29, 2011 at 9:55
  • What I did was create a new class that is called EmployeeDataResultSet, that inherits from ResultSet. Then I added a Property to EmployeeDataResultSet that is the EmployeeData class. Bascially, you encapsulate your Employee data in your ResultSet.
    – Moose
    Apr 29, 2011 at 17:59
1

I think the common concensus would be use a custom return code (integer). As long as the service documents what the possible return codes are, this should be feasible.

5
  • Eek, back to the C-days. Problem with this approach is that it's not very flexible. You can't add custom-error messages like a stacktrace or a message or.. I can see this would be the easy and multi-compatible way out though.
    – cwap
    Mar 26, 2009 at 18:08
  • I have an acquired appreciation for web services that err to the side of simplicity. Mar 26, 2009 at 18:15
  • Although this isn't the highest voted answer I'll accept it. I like the "Webservices should be simple to use" aspect of it, so this is what Ill stick to for now :)
    – cwap
    Mar 26, 2009 at 19:16
  • Yeah, when you've worked with complex web services that have complex responses, you realize that most of it is unnecessary. In most cases, the caller simply needs to know if it succeeded or failed (0 or 1). Mar 26, 2009 at 19:53
  • But what to do when the web service function is supposed to return an object like employeeData when all goes well and should return a error message when there is error.How to return two different objects from same function in different scenarios ? Apr 29, 2011 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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