• wcf接口输出为json格式


    接口定义:

    [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;
    }

  • 相关阅读:
    淘宝网六个质量属性场景
    架构漫谈阅读笔记2
    poj 3304
    poj 2318
    bzoj 4008
    任意模数NTT
    CF623E
    CF712E
    bzoj 1925
    bzoj 4710
  • 原文地址:https://www.cnblogs.com/yyq745201/p/5723638.html
Copyright © 2020-2023  润新知