如何处理在Wcf REST中返回OUT参数的方法

时间:2014-04-07 13:34:15

标签: wcf rest httpclient

由于Wcf REST不支持Out参数。那么如何从重新调整值的方法中获取值。

    [OperationContract(IsOneWay = false)]
    [WebInvoke
     (Method = "GET",
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
     UriTemplate = "xml/getcommentsforvideopage?vid={videoID}&psize={pageSize}&pnum={pageNumber}")]
    IEnumerable<DTOUserComment> GetCommentsForVideoPage(
        // out int resultCount,
                    int videoID, string pageSize,
                    string pageNumber);

如下所示实施------------------

    public IEnumerable<DTOUserComment> GetCommentsForVideoPage(
            int videoID, string pageSize,
            string pageNumber)
    {
        int? pageSizeInt = pageSize.ParseNullableInt();
        int? pageNumberInt = pageNumber.ParseNullableInt();
        IEnumerable<DTOUserComment> results = null;

        // TODO
        int resultCount;

        try
        {
            results = searcher.GetCommentsForVideoPage(**out resultCount,** videoID, pageSizeInt, pageNumberInt);
        }
        catch (UnauthorizedAccessException ex)
        {
            Logger.Write(ex);
        }
        catch (Exception ex)
        {
            Logger.Write(ex);
        }

        return results;
    }  

客户端我使用HttpClient消耗 -

public IEnumerable<UserComment> CommentsPage(int videoId, int startRowIndex, int maximumRows)
    {
        int pageSize = maximumRows;
        if (maximumRows < 1) { maximumRows = 1; }
        int startingPage = (startRowIndex / maximumRows) + 1;
        using (var client = new HttpClient())
        {
                HttpResponseMessage response = client.GetAsync("http://localhost:25252/MyRESTService.svc/xml/getcommentsforvideopage?vid=" + videoId + "&psize=" + pageSize + "&pnum=" + startingPage).Result;
                string strJson = response.Content.ReadAsStringAsync().Result;
                JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
                var data = JObject.Parse(strJson)["GetCommentsForVideoPageResult"];//JSON array starts with "GetCommentsForVideoPageResult"
                IEnumerable<UserComment> comments = JsonConvert.DeserializeObject<IEnumerable<UserComment>>(data.ToString(),settings);
                return comments;
            }
}

1 个答案:

答案 0 :(得分:1)

做这些事。

1.创建一个像这样的包装类

public class WrappedDTOUserComment
{
    public IEnumerable<DTOUserComment> Comments { get; set; }
    public int CommentCount { get; set; }

}

2.然后改变这样的方法

public WrappedDTOUserComment GetCommentsForVideoPage(
        int videoID, string pageSize,
        string pageNumber)
{
    int? pageSizeInt = pageSize.ParseNullableInt();
    int? pageNumberInt = pageNumber.ParseNullableInt();
    WrappedDTOUserComment data = null;
    IEnumerable<DTOUserComment> results = null;
    // TODO
    int resultCount;

    try
    {
        results = searcher.GetCommentsForVideoPage(**out resultCount,** videoID, pageSizeInt, pageNumberInt);
    }
    catch (UnauthorizedAccessException ex)
    {
        Logger.Write(ex);
    }
    catch (Exception ex)
    {
        Logger.Write(ex);
    }
    data.Comments = results;
    data.CommentCount = resultCount;
    return data;
}

3.界面将是

[OperationContract(IsOneWay = false)]
[WebInvoke
 (Method = "GET",
 ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
 UriTemplate = "xml/getcommentsforvideopage?vid={videoID}&psize={pageSize}&pnum={pageNumber}")]
WrappedDTOUserComment GetCommentsForVideoPage(
    // out int resultCount,
                int videoID, string pageSize,
                string pageNumber);