private Message CreateValidResponse(WebOperationContext context, object value) { switch (context.IncomingRequest.Accept) { case "application/xml": return(context.CreateXmlResponse(value.ToString())); case "application/json": return(context.CreateJsonResponse(value.ToString())); default: return(context.CreateTextResponse(value.ToString())); } }
public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { if (version != MessageVersion.None || error == null) { return; } // If the exception is not derived from FaultException and the fault message is already present // then only another error handler could have provided the fault so we should not replace it FaultException errorAsFaultException = error as FaultException; if (errorAsFaultException == null && fault != null) { return; } try { if (error is IWebFaultException) { IWebFaultException webFaultException = (IWebFaultException)error; WebOperationContext context = WebOperationContext.Current; context.OutgoingResponse.StatusCode = webFaultException.StatusCode; string operationName; if (OperationContext.Current.IncomingMessageProperties.TryGetValue <string>(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName, out operationName)) { OperationDescription description = this.contractDescription.Operations.Find(operationName); bool isXmlSerializerFaultFormat = WebHttpBehavior.IsXmlSerializerFaultFormat(description); if (isXmlSerializerFaultFormat && WebOperationContext.Current.OutgoingResponse.Format == WebMessageFormat.Json) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.JsonFormatRequiresDataContract, description.Name, description.DeclaringContract.Name, description.DeclaringContract.Namespace))); } WebMessageFormat?nullableFormat = !isXmlSerializerFaultFormat ? context.OutgoingResponse.Format : WebMessageFormat.Xml; WebMessageFormat format = nullableFormat.HasValue ? nullableFormat.Value : this.webHttpBehavior.GetResponseFormat(description); if (webFaultException.DetailObject != null) { switch (format) { case WebMessageFormat.Json: fault = context.CreateJsonResponse(webFaultException.DetailObject, new DataContractJsonSerializer(webFaultException.DetailType, webFaultException.KnownTypes)); break; case WebMessageFormat.Xml: if (isXmlSerializerFaultFormat) { fault = context.CreateXmlResponse(webFaultException.DetailObject, new XmlSerializer(webFaultException.DetailType, webFaultException.KnownTypes)); } else { fault = context.CreateXmlResponse(webFaultException.DetailObject, new DataContractSerializer(webFaultException.DetailType, webFaultException.KnownTypes)); } break; } } else { HttpResponseMessageProperty property; if (OperationContext.Current.OutgoingMessageProperties.TryGetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name, out property) && property != null) { property.SuppressEntityBody = true; } if (format == WebMessageFormat.Json) { fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); } } } else { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.OperationNameNotFound)); } } else { fault = CreateHtmlResponse(error); } } catch (Exception ex) { if (Fx.IsFatal(ex)) { throw; } if (System.ServiceModel.DiagnosticUtility.ShouldTraceWarning) { System.ServiceModel.DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR2.GetString(SR2.HelpPageFailedToCreateErrorMessage)), TraceEventType.Warning); } WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; fault = CreateHtmlResponse(ex); } }