开发者

How should I return status from my WCF service?

My WCF service has various methods that return integers, strings, lists, and streams. I'd like to add a str开发者_开发问答ing 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.


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!).


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜