How do I return multiple results from a web method?
I am developing a winforms application. One of my forms accepts user input and calls a web service to add the input to the DB. The input must be unique, but I don't know if it is unique on the client side. I am submitting an input to WS and is responsible for adding it to the DB or informing the client that the input already exists.
What is the correct way to implement this?
Should I make two calls to WS, one to see if it is unique and one to insert into the DB? I am experiencing synchronization problems + two round trips along the border.
Should I return an enum, ValueNotUnique and ValueInsertedSuccessfully?
Or perhaps throw an exception? This doesn’t sound like it from a performance standpoint, and I don’t like using exceptions for things I already know that might not work.
Is there a good design for this mess? Help rate, thanks in advance!
a source to share
I would probably do something like this:
- define an enumeration of result values; ValueAlreadyExists, ValueInserted, etc.
- define the type of the returned object which will include
- the result of the operation, since this type of enumeration
- if the value already exists - perhaps something like an identifier or even some data
- if the value was inserted successfully, the new identifier
So, you will have:
public enum OpResult
{
ValueInserted,
ValueAlreadyExists
}
and the type of the result:
public class ResponseType
{
public OpResult Result { get; set; }
public int UniqueID { get; set; }
}
With this approach, you can easily
- expand the enumeration and add more possible results for your operation.
- expand the response type and add more information if you need it
a source to share
Your web method can return a custom class with multiple properties. Web services are not required to return primitive or atomic types. Check your custom Serializable class and make sure any properties are also serializable, etc.
Alternatively, you can throw an exception if you are looking at cases where you did not insert errors and caught the exception in the calling application.
a source to share