public void AddElementsToBodyTest()
        {
            Message message = MessageUtilityTests.GetSampleMessage();
            List <KeyValuePair <string, string> > queryOptions = MessageUtilityTests.GetSampleQueryOptions();

            MessageUtility.AddMessageQueryOptions(ref message, queryOptions);
            MessageUtilityTests.CompareSampleMessage(message);
        }
예제 #2
0
        /// <summary>
        /// Converts the specified <paramref name="parameters"/> into an outbound
        /// <see cref="Message"/>. For query requests with query properties, stores the query
        /// parameters either in the To/Via or the message body.
        /// </summary>
        /// <param name="messageVersion">The version of the message to use.</param>
        /// <param name="parameters">The parameters passed to the client operation.</param>
        /// <returns>The message to send.</returns>
        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
        {
            Message request = this._innerFormatter.SerializeRequest(messageVersion, parameters);

            object queryProperty             = null;
            object includeTotalCountProperty = null;

            if (OperationContext.Current != null)
            {
                OperationContext.Current.OutgoingMessageProperties.TryGetValue(WebDomainClient <object> .QueryPropertyName, out queryProperty);
                OperationContext.Current.OutgoingMessageProperties.TryGetValue(WebDomainClient <object> .IncludeTotalCountPropertyName, out includeTotalCountProperty);
            }

            List <KeyValuePair <string, string> > queryOptions = new List <KeyValuePair <string, string> >();

            if (queryProperty != null)
            {
                foreach (ServiceQueryPart queryPart in QuerySerializer.Serialize((IQueryable)queryProperty))
                {
                    queryOptions.Add(
                        new KeyValuePair <string, string>(
                            queryPart.QueryOperator,
                            queryPart.Expression));
                }
            }

            if (includeTotalCountProperty != null)
            {
                queryOptions.Add(
                    new KeyValuePair <string, string>("includeTotalCount", includeTotalCountProperty.ToString()));
            }

            if (queryOptions.Count > 0)
            {
                Debug.Assert(OperationContext.Current != null, "OpeartionContext.Current cannot be null at this point.");
                if (MessageUtility.IsHttpPOSTMethod(OperationContext.Current.OutgoingMessageProperties))
                {
                    MessageUtility.AddMessageQueryOptions(ref request, queryOptions);
                }
                else
                {
                    MessageUtility.AddQueryToUrl(ref request, queryOptions);
                }
            }

            if (request.Headers.To.AbsoluteUri.Length > WebHttpQueryClientMessageFormatter.MaximumUriLength)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resource.WebDomainClient_MaximumUriLengthExceeded,
                              WebHttpQueryClientMessageFormatter.MaximumUriLength));
            }

            return(request);
        }
        public void BodyWriter_UnitTests()
        {
            Message originalMessage = MessageUtilityTests.GetSampleMessage();
            List <KeyValuePair <string, string> > queryOptions = MessageUtilityTests.GetSampleQueryOptions();
            Message newMessage = originalMessage;

            MessageUtility.AddMessageQueryOptions(ref newMessage, queryOptions);

            // messages are not the same
            Assert.AreNotEqual(originalMessage, newMessage);

            // headers are the same
            UnitTestHelper.AssertListsAreEqual <MessageHeaderInfo>(originalMessage.Headers, newMessage.Headers,
                                                                   (s, h) =>
            {
                return(s + ", " + h.Name);
            });

            // properties are the same
            // workaround for a strange property bug
            originalMessage.Properties.AllowOutputBatching = false;
            UnitTestHelper.AssertListsAreEqual <KeyValuePair <string, object> >(originalMessage.Properties, newMessage.Properties,
                                                                                (s, p) =>
            {
                return(s + ", " + p.ToString());
            });

            // version matches
            Assert.AreEqual(originalMessage.Version, newMessage.Version);

            // initial state check
            Assert.AreEqual(MessageState.Created, originalMessage.State);
            Assert.AreEqual(originalMessage.State, newMessage.State);

            // write message
            MessageUtilityTests.CompareSampleMessage(newMessage);

            // state, the original message should have been closed due to the write
            Assert.AreEqual(MessageState.Closed, originalMessage.State);

            // make sure to clean up
            newMessage.Close();
        }