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; } }
时间: 2024-10-06 07:38:38