当前位置: 首页>>代码示例>>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;未经允许,请勿转载。