/// <summary>
        /// An extension method for getting a <see cref="HttpResponseMessage"/> instance
        /// from an <see cref="Message"/> instance.
        /// </summary>
        /// <param name="message">The <see cref="Message"/> instance from which to
        /// get the <see cref="HttpResponseMessage"/> instance.</param>
        /// <returns>The <see cref="HttpResponseMessage"/> instance.</returns>
        public static HttpResponseMessage ToHttpResponseMessage(this Message message)
        {
            if (message == null)
            {
                throw FxTrace.Exception.ArgumentNull("message");
            }

            HttpResponseMessage httpResponseMessage = HttpResponseMessageProperty.GetHttpResponseMessageFromMessage(message);

            if (httpResponseMessage == null)
            {
                HttpResponseMessageProperty responseMessageProperty = message.Properties.GetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name);
                if (responseMessageProperty == null)
                {
                    throw FxTrace.Exception.AsError(
                              new InvalidOperationException(
                                  SR.MissingHttpMessageProperty(
                                      ToHttpResponseMessageMethodName,
                                      HttpResponseMessagePropertyTypeName)));
                }

                httpResponseMessage = CreateResponseMessage(message, responseMessageProperty);
            }

            return(httpResponseMessage);
        }
            HttpResponseMessage CreateHttpResponseMessage(Message message)
            {
                HttpResponseMessage httpResponseMessage = HttpResponseMessageProperty.GetHttpResponseMessageFromMessage(message);

                if (httpResponseMessage == null)
                {
                    HttpResponseMessageProperty property = message.Properties.GetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name);
                    httpResponseMessage            = new HttpResponseMessage();
                    httpResponseMessage.StatusCode = message.IsFault ? HttpStatusCode.InternalServerError : HttpStatusCode.OK;
                    this.httpOutput.ConfigureHttpResponseMessage(message, httpResponseMessage, property);
                }

                return(httpResponseMessage);
            }
Exemplo n.º 3
0
            public bool SendResponse()
            {
                if (onSendCompleted == null)
                {
                    onSendCompleted = Fx.ThunkCallback(new AsyncCallback(OnSendResponseCompletedCallback));
                }

                bool success = false;

                try
                {
                    this.closeOutputAfterReply = context.PrepareReply(ref this.responseMessage);
                    if (onHttpPipelineSend == null)
                    {
                        onHttpPipelineSend = new Action <object, HttpResponseMessage>(OnHttpPipelineSendCallback);
                    }

                    if (context.httpPipeline.SendAsyncReply(this.responseMessage, onHttpPipelineSend, this) == AsyncCompletionResult.Queued)
                    {
                        //// In Async send + HTTP pipeline path, we will send the response back after the result coming out from the pipeline.
                        //// So we don't need to call it here.
                        success = true;
                        return(false);
                    }

                    HttpResponseMessage httpResponseMessage = null;

                    if (this.context.HttpMessagesSupported)
                    {
                        httpResponseMessage = HttpResponseMessageProperty.GetHttpResponseMessageFromMessage(this.responseMessage);
                    }

                    return(this.SendResponseCore(httpResponseMessage, out success));
                }
                finally
                {
                    if (!success && this.message != null &&
                        !object.ReferenceEquals(this.message, this.responseMessage))
                    {
                        this.responseMessage.Close();
                    }
                }
            }
            protected override void SendReplyCore(Message message, TimeSpan timeout)
            {
                this.TraceProcessResponseStart();
                ThreadTrace.Trace("Begin sending http reply");
                HttpOutput httpOutput = this.GetHttpOutput(message);

                HttpResponseMessage response = HttpResponseMessageProperty.GetHttpResponseMessageFromMessage(message);

                if (response != null)
                {
                    httpOutput.Send(response, timeout);
                }
                else
                {
                    httpOutput.Send(timeout);
                }

                ThreadTrace.Trace("End sending http reply");
                this.TraceProcessResponseStop();
            }