예제 #1
0
        private static Message ConfigureResponseMessage(Message message)
        {
            if (message == null)
            {
                return(null);
            }

            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();

            HttpResponseMessage httpResponseMessage = message.ToHttpResponseMessage();

            if (httpResponseMessage == null)
            {
                responseProperty.StatusCode         = HttpStatusCode.InternalServerError;
                responseProperty.SuppressEntityBody = true;
            }
            else
            {
                responseProperty.StatusCode = httpResponseMessage.StatusCode;
                HttpResponseHeaders responseHeaders = httpResponseMessage.Headers;
                if (responseHeaders != null)
                {
                    foreach (var entry in responseHeaders)
                    {
                        foreach (var value in entry.Value)
                        {
                            responseProperty.Headers.Add(entry.Key, value);
                        }
                    }
                }

                if (httpResponseMessage.Content == null || httpResponseMessage.Content.Headers.ContentLength == 0)
                {
                    responseProperty.SuppressEntityBody = true;
                }
                else
                {
                    foreach (var entry in httpResponseMessage.Content.Headers)
                    {
                        foreach (var value in entry.Value)
                        {
                            responseProperty.Headers.Add(entry.Key, value);
                        }
                    }
                }
            }

            message.Properties.Clear();
            message.Headers.Clear();

            message.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);

            return(message);
        }
            private static HttpResponseMessage GetHttpResponseMessageOrThrow(Message message)
            {
                HttpResponseMessage response = message.ToHttpResponseMessage();

                if (response == null)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.MessageInvalidForHttpMessageEncoder,
                                  httpMessageBindingClassName,
                                  HttpMessageExtensionMethods.ToMessageMethodName,
                                  httpResponseMessageClassName));
                }

                return(response);
            }