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

wcf服务返回json

时间:2015-04-01 14:50:14      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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

wcf服务返回json

标签:

原文地址:http://www.cnblogs.com/han1157/p/4383640.html

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