public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) { if (type == typeof(HttpError)) { HttpError error = (HttpError)value; OperationOutcome opOutcome = new OperationOutcome(); List <string> locations = new List <string>(); if (!string.IsNullOrEmpty(error.StackTrace)) { locations.Add(error.StackTrace); } opOutcome.Issue.Add(new OperationOutcome.IssueComponent() { Severity = OperationOutcome.IssueSeverity.Fatal, Diagnostics = error.Message, Code = OperationOutcome.IssueType.Exception, Location = locations }); string logMessage = string.Format("An error with FHIR STU3 ocurred: {0}\nError Detail: {1}\n{2}\n{3}", error.Message, error.MessageDetail, error.ExceptionMessage, error.StackTrace); Log.For(this).Error(logMessage); value = opOutcome; type = opOutcome.GetType(); } // Return content-type was set by SetDefaultContentType(), just check if that determined that it should be XML bool returnXml = ContentType.XML_CONTENT_HEADERS.Contains(content.Headers.ContentType.MediaType); return(System.Threading.Tasks.Task.Factory.StartNew(() => { if (type == typeof(Resource) || type.IsSubclassOf(typeof(Resource))) { if (returnXml) { XmlWriter writer = new XmlTextWriter(writeStream, Encoding.UTF8); FhirSerializer.SerializeResource((Resource)value, writer); writer.Flush(); } else { StreamWriter writer = new StreamWriter(writeStream); JsonWriter jsonwriter = new JsonTextWriter(writer); Resource resource = (Resource)value; FhirSerializer.SerializeResource(resource, jsonwriter); writer.Flush(); } } else { throw new NotSupportedException(String.Format("Cannot write unsupported type {0} to body", type.Name)); } })); }