Message CreateHtmlResponse(Exception error)
        {
            // Note: WebOperationContext may not be present in case of an invalid HTTP request
            Uri helpUri = null;

            if (WebOperationContext.Current != null)
            {
                helpUri = this.webHttpBehavior.HelpUri != null?UriTemplate.RewriteUri(this.webHttpBehavior.HelpUri, WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Host]) : null;
            }
            StreamBodyWriter bodyWriter;

            if (this.includeExceptionDetailInFaults)
            {
                bodyWriter = StreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage(helpUri, error).Save(s, SaveOptions.OmitDuplicateNamespaces));
            }
            else
            {
                bodyWriter = StreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage(helpUri, null).Save(s, SaveOptions.OmitDuplicateNamespaces));
            }
            Message response = new HttpStreamMessage(bodyWriter);

            response.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);

            HttpResponseMessageProperty responseProperty = GetResponseProperty(WebOperationContext.Current, response);

            if (!responseProperty.HasStatusCodeBeenSet)
            {
                responseProperty.StatusCode = HttpStatusCode.BadRequest;
            }
            responseProperty.Headers[HttpResponseHeader.ContentType] = Atom10Constants.HtmlMediaType;
            return(response);
        }
Exemplo n.º 2
0
        public Message CreateTextResponse(Action <TextWriter> textWriter, string contentType, Encoding encoding)
        {
            if (textWriter == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("textWriter");
            }
            if (contentType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType");
            }
            if (encoding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding");
            }

            Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter((stream) =>
            {
                using (TextWriter writer = new StreamWriter(stream, encoding))
                {
                    textWriter(writer);
                }
            }));

            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
            AddContentType(contentType, null);
            return(message);
        }
Exemplo n.º 3
0
        public Message CreateTextResponse(string text, string contentType, Encoding encoding)
        {
            if (text == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("text");
            }
            if (contentType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType");
            }
            if (encoding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding");
            }

            Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter((stream) =>
            {
                byte[] preamble = encoding.GetPreamble();
                if (preamble.Length > 0)
                {
                    stream.Write(preamble, 0, preamble.Length);
                }
                byte[] bytes = encoding.GetBytes(text);
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
            }));

            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
            AddContentType(contentType, null);
            return(message);
        }
Exemplo n.º 4
0
        public static Message CreateMessage(MessageVersion version, string action, string contentType, Stream stream)
        {
            StreamBodyWriter bodyWriter = new StreamBodyWriter(stream);

            Message message = Message.CreateMessage(version, action, bodyWriter);
            message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));

            HttpResponseMessageProperty response = new HttpResponseMessageProperty();
            response.Headers[System.Net.HttpResponseHeader.ContentType] = contentType;
            message.Properties.Add(HttpResponseMessageProperty.Name, response);


            return message;
        }
Exemplo n.º 5
0
        public Message CreateStreamResponse(Action <Stream> streamWriter, string contentType)
        {
            if (streamWriter == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("streamWriter");
            }
            if (contentType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType");
            }
            Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter(streamWriter));

            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
            AddContentType(contentType, null);
            return(message);
        }