WCF post json and return json

在WCF restful 服务中POST json 动态对象并返回JSON动态对象

参考阅读

(42条消息) 【WCF】WCF RESTful(二)——搭建一个REST的服务_哈士奇-CSDN博客

 

源码

@@@code

[OperationContract]

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]

[SwaggerWcfTag(nameof(GetUTCRange))]

[SwaggerWcfContentTypes(new string[] { "text/plain" })]

public Stream GetUTCRange(Stream streamdata)

{

using (StreamReader reader = new StreamReader(stream))

{

string res = reader.ReadToEnd();

reader.Close();

// return res; //读取动态JSON参数

}

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; //修改返回的声明

return new JsonResponse() { code = 1, msg = $"invalid tableName {json.tableName}" }.ToStream();

}

public static Stream ToStream(this JsonResponse json, bool none = true)

{

return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json.Serialize(none)));

}

@@#

注意:请求头要设置为text/plain



 

Swagger测试

 


 

posted @ 2021-12-06 15:22  秦秋随  阅读(210)  评论(0编辑  收藏  举报