void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            HttpResponseMessage responseMessage = (fault == null) ? this.GetDefaultResponse() : fault.ToHttpResponseMessage();
            if (responseMessage == null)
            {
                string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.HttpErrorMessageNullResponse, this.GetType().Name);
                throw new InvalidOperationException(errorMessage);
            }

            this.ProvideResponse(error, responseMessage);
            fault = responseMessage.ToMessage();
        }
        /// <summary>
        /// Called after the operation has returned but before the reply message is sent.
        /// </summary>
        /// <param name="reply">The reply message. This value is null if the operation is one way.</param>
        /// <param name="correlationState">The correlation object returned from the <see cref="AfterReceiveRequest(ref Message, IClientChannel, InstanceContext)"/> method.</param>
        void IDispatchMessageInspector.BeforeSendReply(
            ref Message reply,
            object correlationState)
        {
            if (reply == null)
            {
                throw new ArgumentNullException("reply");
            }

            HttpResponseMessage httpResponse = reply.ToHttpResponseMessage();

            if (httpResponse == null)
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        SR.HttpMessageInspectorNullResponse,
                        this.GetType().Name));
            }

            this.BeforeSendReply(httpResponse, correlationState);
        }
            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;
            }
        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;
        }