//由于调用 ProvideFault 时,客户端处于阻塞状态,不要在这里进行长时间的操作
public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
{
	//避免敏感信息泄漏,例如:数据库配置, error包含的错误信息应该记录到服务器的日志中,不能显示给客户端
	// FaultException<int> e = new FaultException<int>(123, error.Message);
	DateTime now = DateTime.Now;
	time = now.ToString("yyyyMMddHHmmssfff", DateTimeFormatInfo.InvariantInfo);// "" + now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString();
	string errorMsg = "服务内部错误_" + time;
	// FaultException fe = new FaultException(errorMsg);
	// MessageFault mf = fe.CreateMessageFault();
	// msg = Message.CreateMessage(version, mf, fe.Action);
	//The fault to be returned
	msg = Message.CreateMessage(version, "", errorMsg, new DataContractJsonSerializer(typeof(string)));
	// tell WCF to use JSON encoding rather than default XML
	WebBodyFormatMessageProperty wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
	// Add the formatter to the fault
	msg.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);
	//Modify response
	HttpResponseMessageProperty rmp = new HttpResponseMessageProperty();
	// return custom error code, 400.
	rmp.StatusCode = System.Net.HttpStatusCode.InternalServerError;
	rmp.StatusDescription = "Bad request";
	//Mark the jsonerror and json content
	rmp.Headers[HttpResponseHeader.ContentType] = "application/json";
	rmp.Headers["jsonerror"] = "true";
	//Add to fault
	msg.Properties.Add(HttpResponseMessageProperty.Name, rmp);
}
 
                    详细解决方案
                wcf 错误 抛出json格式的错误消息
热度:491   发布时间:2013-12-21 20:16:01.0
                    相关解决方案
                
                - json 解析有关问题
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- 请教哪里可以上到 struts2-json-plugin-2.1.8的源文件
- java+jquery easy ui + json + struts分页例子
- ssh json jar 有关问题
- json 有关问题
- jquery.load(*json) 解析不已。求帮忙
- json 转成 string解决办法
- json 序列化有错误怎么办
- MVC(Json) 出现提示上载
- json 序列化有关问题?当列名为可变数值时,如果序列化
- HttpWebRequest post json 数据,接受应该怎么获取啊
- jqGrid json 不显示数据,该如何处理
- JQUERY AJAX JSON 有关问题 求大侠指点
- json[0].CodeID值为undefined解决方法
- 求 json 好手
- table2json html table 变换为 json
- php json 不能解析中文字符串?解决方法
- XML,JSON,HTML格式对照
- ajax json data应该如何写
- Struts2 + json+ jquery 兑现三级联动action和jsp代码竟然有小红叉,提示缺双引号,检查了转义符号也没缺啊求解
- 关于AJAX回来 json or html
- 把在线编辑器的代码赋给 JSON 出错的有关问题
- JSON web运用前后端通讯的轻量级结果处理
- json 处置hibernate级联
- C#序列化字符串,对象,xml,json,会合数组,字典
- JSON.FROMOBJECT 处置集合中的日期报错
- Json.fromObject使用务必要有get方法
- JSON 字符串 与 java 对象的变换
- ie6/七/8 window.JSON