码迷,mamicode.com
首页 > Web开发 > 详细

wcf接口输出为json格式

时间:2016-07-31 19:23:46      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

接口定义:

[OperationContract]
[WebInvoke(UriTemplate = "AddTask?taskId={taskId}&processGuid={processGuid}", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream AddTask(Stream request, string taskId, string processGuid);

 

处理完任务,输出流返回的时候加上System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";就行了

/// <summary>
/// 辅助方法,用于输出流
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private Stream GetJsonStream(string str)
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.AutoFlush = true;
sw.Write(str);
ms.Position = 0;
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
return ms;
}

wcf接口输出为json格式

标签:

原文地址:http://www.cnblogs.com/yyq745201/p/5723638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!