コード例 #1
0
        internal static HttpMessageEncodingRequestContext GetContextFromMessage(Message message)
        {
            HttpMessageEncodingRequestContext context = null;

            message.Properties.TryGetValue <HttpMessageEncodingRequestContext>(HttpMessageEncodingRequestContextPropertyName, out context);
            return(context);
        }
コード例 #2
0
            public override void WriteMessage(Message message, Stream stream)
            {
                if (message == null)
                {
                    throw Error.ArgumentNull("message");
                }

                if (stream == null)
                {
                    throw Error.ArgumentNull("stream");
                }

                ThrowIfMismatchedMessageVersion(message);

                message.Properties.Encoder = this;

                HttpResponseMessage response = GetHttpResponseMessageOrThrow(message);

                if (response.Content != null)
                {
                    HttpMessageEncodingRequestContext requestContext =
                        HttpMessageEncodingRequestContext.GetContextFromMessage(message);

                    WriteMessageCore(response, stream, requestContext).Wait();
                }
            }
コード例 #3
0
            public override void WriteMessage(Message message, Stream stream)
            {
                if (message == null)
                {
                    throw Error.ArgumentNull("message");
                }

                if (stream == null)
                {
                    throw Error.ArgumentNull("stream");
                }

                ThrowIfMismatchedMessageVersion(message);

                message.Properties.Encoder = this;

                HttpResponseMessage response = GetHttpResponseMessageOrThrow(message);

                if (response.Content != null)
                {
                    HttpMessageEncodingRequestContext requestContext =
                        HttpMessageEncodingRequestContext.GetContextFromMessage(message);
                    try
                    {
                        response.Content.CopyToAsync(stream)
                        .Catch((info) =>
                        {
                            if (requestContext != null)
                            {
                                requestContext.Exception = info.Exception;
                            }

                            return(info.Throw());
                        })
                        .Wait();
                    }
                    catch (Exception ex)
                    {
                        if (requestContext != null)
                        {
                            requestContext.Exception = ex;
                        }

                        throw;
                    }
                }
            }
コード例 #4
0
            public override ArraySegment <byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
            {
                if (message == null)
                {
                    throw Error.ArgumentNull("message");
                }

                if (bufferManager == null)
                {
                    throw Error.ArgumentNull("bufferManager");
                }

                if (maxMessageSize < 0)
                {
                    throw Error.ArgumentOutOfRange("maxMessageSize", maxMessageSize, SRResources.NonnegativeNumberRequired);
                }

                if (messageOffset < 0)
                {
                    throw Error.ArgumentOutOfRange("messageOffset", messageOffset, SRResources.NonnegativeNumberRequired);
                }

                if (messageOffset > maxMessageSize)
                {
                    throw Error.Argument(String.Empty, SRResources.ParameterMustBeLessThanOrEqualSecondParameter, "messageOffset", "maxMessageSize");
                }

                // TODO: DevDiv2 bug #378887 -- find out how to eliminate this middle buffer
                using (BufferManagerOutputStream stream = new BufferManagerOutputStream(MaxSentMessageSizeExceededResourceStringName, 0, maxMessageSize, bufferManager))
                {
                    int num;
                    stream.Skip(messageOffset);
                    WriteMessage(message, stream);

                    byte[] buffer = stream.ToArray(out num);
                    ArraySegment <byte> messageData = new ArraySegment <byte>(buffer, 0, num - messageOffset);

                    // ToArray transfers full ownership of buffer to us, meaning we are responsible for returning it to BufferManager.
                    // But we must delay that release until WCF has finished with the buffer we are returning from this method.
                    HttpMessageEncodingRequestContext requestContext = HttpMessageEncodingRequestContext.GetContextFromMessage(message);
                    Contract.Assert(requestContext != null);
                    requestContext.BufferManager  = bufferManager;
                    requestContext.BufferToReturn = buffer;

                    return(messageData);
                }
            }
コード例 #5
0
            private static async Task WriteMessageCore(
                HttpResponseMessage response,
                Stream stream,
                HttpMessageEncodingRequestContext requestContext)
            {
                try
                {
                    await response.Content.CopyToAsync(stream);
                }
                catch (Exception ex)
                {
                    if (requestContext != null)
                    {
                        requestContext.Exception = ex;
                    }

                    throw;
                }
            }