C# Web Service - Error Handling

Associate
Joined
28 Nov 2004
Posts
1,237
Location
Birmingham
Hi there!
I'm new to web services and I'm building my 1st one (the asmx way). It's all working nicely but I want to be able to present my error messages (such as login error etc) a bit more nicely.
My "GetResults" method pulls back data from the database upon logging in successfully and I'm compiling a List<> of my Items and returning them.
However, if there is an error, I want to instead return an instance of my "error" object to the user. At the moment, I can't change the return type of the method so the two objects are clashing. Any help?
 
Just encapsulate your return object(s) within another object, and give that object an Error property.

E.g

Public class ReturnObject
{
public List<MyObject> MyObjects {get;set;}
public MyError Error {get;set;}
}
 
Last edited:
1. You can throw an exception in you service, and it'll get serialized into a standard soap <fault> message to the client. That's the standardized SOAP way of dealing with exceptions.

2. Putting an "Error" property in the return type is pretty common, and works.

3. Another way is to go the WCF route. In which case you could use datacontract serialization and have a base return value class and have your actual return types inherit it. Then you can add those to your base class' known types, and they'll get exposed in the wsdl as well, so you can pass them just like any regular polymorphic reference.

I don't think it's possible to use DataContracts with legacy .asmx service though (?)
 
thanks guys!
i will go down the wcf route. for the moment tho, im sticking with asmx.

i'll try out the suggested method! nice one!
 
Back
Top Bottom