wcf服务返回json

private static void CreateErrorReply(OperationContext operationContext, string key, HttpStatusCode statusCode, ResponseBase info=null, string description = null)
        {
            
            using (Message reply = Message.CreateMessage(MessageVersion.None, "", info, new DataContractJsonSerializer(typeof(ResponseBase))))
            {
                var wbfProp = new System.ServiceModel.Channels.WebBodyFormatMessageProperty(WebContentFormat.Json);
                // 构造HTTP响应消息
                HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty()
                {
                    StatusCode = statusCode,
                    StatusDescription = description ?? "", 
                    SuppressEntityBody=false, 
                };
 
                reply.Properties.Add(HttpResponseMessageProperty.Name, responseProp);
                reply.Properties.Add(WebBodyFormatMessageProperty.Name, wbfProp);
 
 
                operationContext.RequestContext.Reply(reply);
                
                // 因为配置使用transferMode="StreamedRequest",如果请求不关闭,还会进入这个类,导致这个方法被执行两次,不能返回正常的错取码
                operationContext.RequestContext.Close();
                operationContext.RequestContext = null;
            }
        }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。