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的更多相关文章

  1. 【.net 深呼吸】聊聊WCF服务返回XML或JSON格式数据

    有时候,为了让数据可以“跨国经营”,尤其是HTTP Web有关的东东,会将数据内容以 XML 或 JSON 的格式返回,这样一来,不管客户端平台是四大文明古国,还是处于蒙昧时代的原始部落,都可以使用这 ...

  2. Android访问WCF服务(使用json实现参数传递)

    经过多日努力, 终于勉强弄明白了Android访问WCF服务的方法. 服务端实现 一, 实现服务. 操作契约 [ServiceContract] public interface IService { ...

  3. jQuery调用WCF服务传递JSON对象

    下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...

  4. Spring MVC Rest服务 返回json报406错误的解决办法

    @ResponseBody & @RequestBody @RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象. ...

  5. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...

  6. ajax请求web服务返回json格式

    由于.net frameword3.5以上添加了对contenttype的检查,当ajax发送请求时,如果设置了contenttype为json,那么请求webservice时,会自动将返回的内容转为 ...

  7. Downloader调用WCF服务返回文件

    Generator using System; using System.Collections.Generic; using System.IO; namespace Downloader { pu ...

  8. jersey构建rest服务返回json数据

    1.  eclipse 创建 dynamic web project 2.  将jersey相关jar包放到libs目录下 3. web.xml 增加 jersey 相关内容 <?xml ver ...

  9. WCF返回JSON的详细配置

    开发环境:VS2008,c# 1.新建个WCF服务网站 文件-新建-网站-WCF服务 2,运行一下,提示配置WEB.CONFIG,点击确认. 3,打开web.config增加如下节点: <ser ...

随机推荐

  1. tomcat文件服务配置

    <Host name="localhost"  appBase=""           unpackWARs="true" auto ...

  2. Swift - 计算文本高度

    Swift - 计算文本高度 效果 源码 // // String+StringHeight.swift // StringHeight // // Created by YouXianMing on ...

  3. Flv.js

    Flv.js 是 HTML5 Flash 视频(FLV)播放器,纯原生 JavaScript 开发,没有用到 Flash.由 bilibili 网站开源. 该项目依托于 Media Source Ex ...

  4. Android——GridView(网格视图)相关知识总结贴

    Android API中文文档GridView http://www.apkbus.com/android-14131-1-1.html   Android API 中文 (15) —— GridVi ...

  5. ibatis返回map列表

    ibatis返回map列表 1. resultClass="java.util.HashMap"   <select id="queryCustmerCarNoBy ...

  6. 关于MYSQL中like 检索汉字问题。

    mysql中like’中国‘查询不出来结果 如果 like‘zg’就看查询出来结果. 查询了半天是由于mysql中编码的原因 解决办法: 进入mysql 输入命令:show variables lik ...

  7. Why GUID primary keys are a database’s worst nightmare

    http://csharptest.net/1250/why-guid-primary-keys-are-a-databases-worst-nightmare/ When you ask most ...

  8. Activity intent经常使用的 FLAG

    Intent.FLAG_ACTIVITY_NEW_TASK 默认的跳转类型,会重新创建一个新的Activity,不过与这种情况,比方说Task1中有A,B,C三个Activity,此时在C中启动D的话 ...

  9. Apache shiro

    Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权. 学习博客:http://jinnianshilongnian.iteye.com/blog/2018398

  10. SQL Server への接続を許可するファイアーウォール設定

    netsh advfirewall firewall add rule name="SQL Server Browser" protocol=UDP dir=in localpor ...