0

My WCF service has various methods that return integers, strings, lists, and streams. I'd like to add a string or enum or ??? Indicating the status of the call upon completion. It could be as simple as a bool indicating success or failure.I'm not sure yet. The question is, how do I implement this ? Should I specify an out parameter in the signature ? Is there a better, more commonly used way to accomplish this ?

update 1 If an exception occurs, it will be handled, logged, etc. One scenario that comes to mind is when a user submits something for which there is no result to return. For example, the state abbreviation of ZZ will return nothing but I still want to indicate the method ran successfully.

DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233
  • To indicate a failure, just throw an exception. If you like to add more information on the reason og the failure, you can use use service Fault: http://msdn.microsoft.com/en-us/library/ms732013.aspx. – seva titov Oct 13 '11 at 01:54

3 Answers3

1

If the "status" you wish to indicate is various kinds of error, then, for a SOAP-based service, you should use SOAP Faults.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
1

If an error happens - return a SOAP fault - that much is clear.

Now if something else happens, or if you want to somehow inform the user of a status / a total number of rows or something, the best approach is to have a response class that contains all that information:

[DataContract]
public class YourMethodResponse
{
    [DataMember]
    public List<string> ActualResults;

    [DataMember]
    public int StatusOfOperation;

    [DataMember]
    public int TotalRowsUpdated;
}

or whatever makes sense for you.

ThHen, instead of this operation

[OperationContract]
public List<string> UpdateStrings();

just use one that returns that response class:

[OperationContract]
public YourMethodResponse UpdateStrings();

That way, you have a clear interface, everything's nicely and properly handled, and you have total flexibility as to what you want to return (flags, enums, whatever you can dream up!).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • @ marc_s - Thanks. I did see examples of this approach. I steered clear because it was my understanding this would not work with streams. Is that correct? If correct, how would you handle the same scenario but with the actual results being returned as a stream? – DenaliHardtail Oct 13 '11 at 13:58
0

You might want to take advantage of the System.ServiceModel.Web.OutgoingWebResponseContext class as well as this question that addresses a similar scenario. Hope it helps.

Community
  • 1
  • 1
Nano Taboada
  • 4,148
  • 11
  • 61
  • 89