首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

wcf 错误 抛出json格式的错误消息

2013-12-21 
wcf 异常 抛出json格式的异常消息//由于调用 ProvideFault 时,客户端处于阻塞状态,不要在这里进行长时间的

wcf 异常 抛出json格式的异常消息

//由于调用 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 returnedmsg = Message.CreateMessage(version, "", errorMsg, new DataContractJsonSerializer(typeof(string)));// tell WCF to use JSON encoding rather than default XMLWebBodyFormatMessageProperty wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);// Add the formatter to the faultmsg.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);//Modify responseHttpResponseMessageProperty rmp = new HttpResponseMessageProperty();// return custom error code, 400.rmp.StatusCode = System.Net.HttpStatusCode.InternalServerError;rmp.StatusDescription = "Bad request";//Mark the jsonerror and json contentrmp.Headers[HttpResponseHeader.ContentType] = "application/json";rmp.Headers["jsonerror"] = "true";//Add to faultmsg.Properties.Add(HttpResponseMessageProperty.Name, rmp);}

热点排行