當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.ToMessage方法代碼示例

本文整理匯總了C#中System.Net.Http.HttpRequestMessage.ToMessage方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.ToMessage方法的具體用法?C# HttpRequestMessage.ToMessage怎麽用?C# HttpRequestMessage.ToMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.ToMessage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ToHttpRequestMessage_Returns_The_Original_HttpRequestMessage

 public void ToHttpRequestMessage_Returns_The_Original_HttpRequestMessage()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     Message wcfRequest = request.ToMessage();
     HttpRequestMessage requestRoundTripped = wcfRequest.ToHttpRequestMessage();
     Assert.IsNotNull(requestRoundTripped, "Message.ToHttpRequestMessage should not have returned null.");
     Assert.AreSame(request, requestRoundTripped, "Message.ToHttpRequestMessage should have returned the orignal instance of HttpRequestMessage.");
 }
開發者ID:AlexZeitler,項目名稱:WcfHttpMvcFormsAuth,代碼行數:8,代碼來源:HttpMessageExtensionMethodsTests.cs

示例2: ToHttpRequestMessage_Does_Not_Change_Message_State

 public void ToHttpRequestMessage_Does_Not_Change_Message_State()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     Message wcfRequest = request.ToMessage();
     MessageState state = wcfRequest.State;
     wcfRequest.ToHttpRequestMessage();
     Assert.AreEqual(state, wcfRequest.State, "Message.State should be the same before and after calling Message.ToHttpRequestMessage.");
 }
開發者ID:nuxleus,項目名稱:WCFWeb,代碼行數:8,代碼來源:HttpMessageExtensionMethodsTests.cs

示例3: ToHttpRequestMessage_Can_Be_Called_Multiple_Times

        public void ToHttpRequestMessage_Can_Be_Called_Multiple_Times()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            Message wcfRequest = request.ToMessage();
            HttpRequestMessage requestRoundTripped = wcfRequest.ToHttpRequestMessage();
            Assert.IsNotNull(requestRoundTripped, "Message.ToHttpRequestMessage should not have returned null.");
            Assert.AreSame(request, requestRoundTripped, "Message.ToHttpRequestMessage should have returned the orignal instance of HttpRequestMessage.");

            HttpRequestMessage requestRoundTrippedAgain = wcfRequest.ToHttpRequestMessage();
            Assert.IsNotNull(requestRoundTrippedAgain, "Message.ToHttpRequestMessage should not have returned null.");
            Assert.AreSame(request, requestRoundTrippedAgain, "Message.ToHttpRequestMessage should have returned the orignal instance of HttpRequestMessage.");
        }
開發者ID:nuxleus,項目名稱:WCFWeb,代碼行數:12,代碼來源:HttpMessageExtensionMethodsTests.cs

示例4: TraceHttpRequestMessage

        private Message TraceHttpRequestMessage(HttpRequestMessage msg)
        {
            string tracemsg = String.Format(
                "Web Request on URL: {0}\n" +
                "Header: {1}\n" +
                "Body: {2}",
                msg.RequestUri.AbsoluteUri,
                msg,
                msg.Content != null ? msg.Content.ReadAsString() : "(empty)");

            LoggingHelper.TraceLine(tracemsg, LoggingHelper.LogLevel.Info);

            return msg.ToMessage();
        }
開發者ID:ogazitt,項目名稱:TaskStore,代碼行數:14,代碼來源:MessageLoggingHelper.cs

示例5: SelectOperation_Returns_Custom_Operation_Name

        public void SelectOperation_Returns_Custom_Operation_Name()
        {
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
            Message message = httpRequestMessage.ToMessage();

            SHttpOperationSelector selector = new SHttpOperationSelector();
            selector.OnSelectOperationHttpRequestMessage =
                (localHttpRequestMessag) =>
                {
                    Assert.AreSame(httpRequestMessage, localHttpRequestMessag, "The 'OnSelectOperation' method should have been called with the same HttpRequestMessage instance.");
                    return "CustomOperation";
                };

            string returnedOperation = ((IDispatchOperationSelector)selector).SelectOperation(ref message);
            Assert.AreEqual("CustomOperation", returnedOperation, "SelectOperation should have returned the custom operation name.");
        }
開發者ID:nuxleus,項目名稱:WCFWeb,代碼行數:16,代碼來源:HttpOperationSelectorTests.cs

示例6: DeserializeRequest_Receives_Message_And_Parameters

        public void DeserializeRequest_Receives_Message_And_Parameters()
        {
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
            Message wcfMessage = httpRequestMessage.ToMessage();
            object[] messageParameters = new object[] { "hello", 5.0 };

            IDispatchMessageFormatter formatter = new MockHttpMessageFormatter()
            {
                OnDeserializeRequest = (msg, parameters) =>
                {
                    Assert.AreSame(httpRequestMessage, msg, "DeserializeRequest did not receive the HttpRequestMessage we specified");
                    Assert.AreSame(messageParameters, parameters, "DeserializeRequest did not receive the parameters we specified");
                }
            };

            formatter.DeserializeRequest(wcfMessage, messageParameters);
            Assert.IsTrue(((MockHttpMessageFormatter)formatter).WasDeserializeRequestCalled, "DeserializeRequest in derived class was not called");
        }
開發者ID:AlexZeitler,項目名稱:WcfHttpMvcFormsAuth,代碼行數:18,代碼來源:HttpMessageFormatterTests.cs

示例7: ReadMessage

            public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
            {
                if (bufferManager == null)
                {
                    throw Error.ArgumentNull("bufferManager");
                }

                HttpRequestMessage request = new HttpRequestMessage();
                request.Content = new ByteArrayBufferManagerContent(bufferManager, buffer.Array, buffer.Offset, buffer.Count);
                if (!String.IsNullOrEmpty(contentType))
                {
                    request.Content.Headers.Add(ContentTypeHeaderName, contentType);
                }

                Message message = request.ToMessage();
                message.Properties.Encoder = this;

                return message;
            }
開發者ID:haoduotnt,項目名稱:aspnetwebstack,代碼行數:19,代碼來源:HttpMessageEncoderFactory.cs

示例8: AfterReceiveRequest_Receives_HttpRequestMessage

        public void AfterReceiveRequest_Receives_HttpRequestMessage()
        {
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
            Message wcfMessage = httpRequestMessage.ToMessage();
            IClientChannel channel = new MockClientChannel();
            InstanceContext context = new InstanceContext(new MockService1());

            IDispatchMessageInspector inspector = new MockHttpMessageInspector()
            {
                OnAfterReceiveRequest = actualMessage =>
                {
                    Assert.AreSame(httpRequestMessage, actualMessage, "AfterReceiveRequest did not receive the HttpRequestMessage");
                    return /*state*/ null;
                }
            };

            inspector.AfterReceiveRequest(ref wcfMessage, channel, context);
            Assert.IsTrue(((MockHttpMessageInspector)inspector).WasAfterReceiveRequestCalled, "AfterReceiveRequest in derived class was not called");
        }
開發者ID:AlexZeitler,項目名稱:WcfHttpMvcFormsAuth,代碼行數:19,代碼來源:HttpMessageInspectorTests.cs

示例9: TraceHttpRequestMessage

        private Message TraceHttpRequestMessage(HttpRequestMessage msg)
        {
            // get the session from the session header if it's present
            IEnumerable<string> header = new List<string>();
            if (msg.Headers.TryGetValues(HttpApplicationHeaders.Session, out header))
                TraceLog.Session = header.ToArray<string>()[0];

            // trace request
            string messageText = msg.Content != null ? msg.Content.ReadAsStringAsync().Result : "(empty)";
            string tracemsg = String.Format(
                "\n{0} {1}; User-Agent: {2}; Content-Type: {3}; Content-Length: {4}\n" +
                "Body: {5}",
                msg.Method,
                msg.RequestUri.AbsoluteUri,
                msg.Headers.UserAgent,
                msg.Content.Headers.ContentType,
                msg.Content.Headers.ContentLength,
                messageText);
            TraceLog.TraceLine(tracemsg, TraceLog.LogLevel.Detail);
            return msg.ToMessage();
        }
開發者ID:ogazitt,項目名稱:product,代碼行數:21,代碼來源:MessageLoggingHelper.cs

示例10: OnDeserializeRequestExecutesRequestPipeline

        public void OnDeserializeRequestExecutesRequestPipeline()
        {
            SHttpOperationDescription operation = new SHttpOperationDescription() { CallBase = true, ReturnValue = HttpParameter.ResponseMessage };
            IEnumerable<HttpOperationHandler> emptyHandlers = Enumerable.Empty<HttpOperationHandler>();
            OperationHandlerPipeline pipeline = new OperationHandlerPipeline(emptyHandlers, emptyHandlers, operation);
            MOperationHandlerPipeline molePipeline = new MOperationHandlerPipeline(pipeline);
            molePipeline.BehaveAsDefaultValue();

            MOperationHandlerPipelineContext moleContext = new MOperationHandlerPipelineContext();

            HttpRequestMessage setRequest = null;
            object[] setValues = null;
            OperationHandlerPipelineContext setContext = null;
            molePipeline.ExecuteRequestPipelineHttpRequestMessageObjectArray = (request, values) =>
                {
                    setRequest = request;
                    setValues = values;
                    return setContext = moleContext;
                };

            OperationHandlerFormatter formatter = new OperationHandlerFormatter(molePipeline);
            IDispatchMessageFormatter dispatchMessageFormatter = (IDispatchMessageFormatter)formatter;

            Uri uri = new Uri("http://somehost/Fred");
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, uri);
            httpRequest.Content = new StringContent("");

            Message message = httpRequest.ToMessage();
            object[] parameters = new object[0];
            dispatchMessageFormatter.DeserializeRequest(message, parameters);

            HttpAssert.AreEqual(httpRequest, setRequest);
            Assert.IsNotNull(setValues, "Input values were not passed to the pipeline.");
            Assert.AreEqual(0, ((object[])setValues).Length, "Incorrect number of values.");
            Assert.IsNotNull(setContext, "Context was not set.");
        }
開發者ID:nuxleus,項目名稱:WCFWeb,代碼行數:36,代碼來源:OperationHandlerFormatterTests.cs

示例11: ReadMessage

            public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
            {
                if (bufferManager == null)
                {
                    throw Fx.Exception.ArgumentNull("bufferManager");
                }

                // TODO: CSDMAIN 205175 -- reactivate when tracing and logging are available:
                //// if (WebTD.HttpMessageDecodingStartIsEnabled())
                //// {
                ////     WebTD.HttpMessageDecodingStart();
                //// }

                HttpRequestMessage request = new HttpRequestMessage();
                request.Content = new ByteArrayBufferManagerContent(bufferManager, buffer.Array, buffer.Offset, buffer.Count);
                if (!string.IsNullOrEmpty(contentType))
                {
                    request.Content.Headers.Add(ContentTypeHeaderName, contentType);
                }

                Message message = request.ToMessage();
                message.Properties.Encoder = this;

                // TODO: CSDMAIN 205175 -- reactivate when tracing and logging are available:
                //// if (TD.MessageReadByEncoderIsEnabled() && buffer != null)
                //// {
                ////     TD.MessageReadByEncoder(
                ////             EventTraceActivityHelper.TryExtractActivity(message, true),
                ////             buffer.Count,
                ////             this);
                //// }

                // TODO: CSDMAIN 205175 -- reactivate when tracing and logging are available:
                //// if (MessageLogger.LogMessagesAtTransportLevel)
                //// {
                ////     MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive);
                //// }

                return message;
            }
開發者ID:nuxleus,項目名稱:WCFWeb,代碼行數:40,代碼來源:HttpMessageEncoderFactory.cs

示例12: ReadMessage

            public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
            {
                if (bufferManager == null)
                {
                    throw new ArgumentNullException("bufferManager");
                }

                byte[] content = new byte[buffer.Count];
                Array.Copy(buffer.Array, buffer.Offset, content, 0, content.Length);
                bufferManager.ReturnBuffer(buffer.Array);

                HttpRequestMessage request = new HttpRequestMessage();
                var httpContent = new ByteArrayContent(content);
                httpContent.Headers.Clear();

                if (contentType != null)
                {
                    httpContent.Headers.Add("content-type", contentType);
                }

                request.Content = httpContent;

                Message message = request.ToMessage();
                message.Properties.Encoder = this;

                return message;
            }
開發者ID:AlexZeitler,項目名稱:WcfHttpMvcFormsAuth,代碼行數:27,代碼來源:HttpMessageEncoderFactory.cs

示例13: ConfigureRequestMessage

        private static Message ConfigureRequestMessage(Message message)
        {
            if (message == null)
            {
                return null;
            }

            HttpRequestMessageProperty requestProperty;
            if (!message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out requestProperty))
            {
                throw Error.InvalidOperation(
                    SRResources.RequestMissingHttpRequestMessageProperty,
                    HttpRequestMessageProperty.Name,
                    typeof(HttpRequestMessageProperty).Name);
            }

            Uri uri = message.Headers.To;
            if (uri == null)
            {
                throw Error.InvalidOperation(SRResources.RequestMissingToHeader);
            }

            HttpRequestMessage httpRequestMessage = message.ToHttpRequestMessage();
            if (httpRequestMessage == null)
            {
                if (!message.IsEmpty)
                {
                    throw Error.InvalidOperation(SRResources.NonHttpMessageMustBeEmpty, HttpMessageExtensions.ToHttpRequestMessageMethodName, typeof(HttpMessage).Name);
                }

                httpRequestMessage = new HttpRequestMessage();
                Message oldMessage = message;
                message = httpRequestMessage.ToMessage();
                message.Properties.CopyProperties(oldMessage.Properties);
                oldMessage.Close();
            }
            else
            {
                // Clear headers but not properties.
                message.Headers.Clear();
            }

            // Copy message properties to HttpRequestMessage. While it does have the
            // risk of allowing properties to get out of sync they in virtually all cases are
            // read-only so the risk is low. The downside to not doing it is that it isn't
            // possible to access anything from HttpRequestMessage (or OperationContent.Current)
            // which is worse.
            foreach (KeyValuePair<string, object> kv in message.Properties)
            {
                httpRequestMessage.Properties.Add(kv.Key, kv.Value);
            }

            if (httpRequestMessage.Content == null)
            {
                httpRequestMessage.Content = new ByteArrayContent(new byte[0]);
            }
            else
            {
                httpRequestMessage.Content.Headers.Clear();
            }

            message.Headers.To = uri;

            httpRequestMessage.RequestUri = uri;
            httpRequestMessage.Method = HttpMethodHelper.GetHttpMethod(requestProperty.Method);

            WebHeaderCollection headers = requestProperty.Headers;
            foreach (var headerName in headers.AllKeys)
            {
                string headerValue = headers[headerName];
                if (!httpRequestMessage.Headers.TryAddWithoutValidation(headerName, headerValue))
                {
                    httpRequestMessage.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
                }
            }

            return message;
        }
開發者ID:huangw-t,項目名稱:aspnetwebstack,代碼行數:78,代碼來源:HttpMessageEncodingRequestContext.cs

示例14: ConfigureRequestMessage

        private static Message ConfigureRequestMessage(Message message)
        {
            if (message == null)
            {
                return null;
            }

            HttpRequestMessageProperty requestProperty = message.GetHttpRequestMessageProperty();
            if (requestProperty == null)
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        SR.RequestMissingHttpRequestMessageProperty,
                        HttpRequestMessageProperty.Name,
                        typeof(HttpRequestMessageProperty).FullName));
            }

            Uri uri = message.Headers.To;
            if (uri == null)
            {
                throw new InvalidOperationException(SR.RequestMissingToHeader);
            }

            HttpRequestMessage httpRequestMessage = message.ToHttpRequestMessage();
            if (httpRequestMessage == null)
            {
                httpRequestMessage = new HttpRequestMessage();
                httpRequestMessage.Content = new StringContent(String.Empty);
                httpRequestMessage.Content.Headers.ContentLength = 0;
                message.Close();
                message = httpRequestMessage.ToMessage();
            }
            else
            {
                message.Headers.Clear();
                message.Properties.Clear();
                httpRequestMessage.Headers.Clear();
                httpRequestMessage.GetProperties().Clear();
            }

            message.Headers.To = uri;

            httpRequestMessage.RequestUri = uri;
            httpRequestMessage.Method = new HttpMethod(requestProperty.Method);

            foreach (var headerName in requestProperty.Headers.AllKeys)
            {
                if (headerName.StartsWith("content-", StringComparison.OrdinalIgnoreCase) ||
                    headerName.Equals("Allow", StringComparison.OrdinalIgnoreCase) ||
                    headerName.Equals("Expires") ||
                    headerName.Equals("Expires", StringComparison.OrdinalIgnoreCase))
                {
                    httpRequestMessage.Content.Headers.Remove(headerName);
                    httpRequestMessage.Content.Headers.Add(headerName, requestProperty.Headers[headerName]);
                    continue;
                }

                httpRequestMessage.Headers.Remove(headerName);
                httpRequestMessage.Headers.Add(headerName, requestProperty.Headers[headerName]);
            }

            return message;
        }
開發者ID:AlexZeitler,項目名稱:WcfHttpMvcFormsAuth,代碼行數:64,代碼來源:HttpMessageEncodingRequestContext.cs

示例15: ReadMessage

            public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
            {
                if (stream == null)
                {
                    throw Error.ArgumentNull("stream");
                }

                HttpRequestMessage request = new HttpRequestMessage();
                request.Content = new StreamContent(stream);
                if (!String.IsNullOrEmpty(contentType))
                {
                    request.Content.Headers.TryAddWithoutValidation(ContentTypeHeaderName, contentType);
                }

                Message message = request.ToMessage();
                message.Properties.Encoder = this;

                return message;
            }
開發者ID:chrissimon-au,項目名稱:aspnetwebstack,代碼行數:19,代碼來源:HttpMessageEncoderFactory.cs


注:本文中的System.Net.Http.HttpRequestMessage.ToMessage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。