Example #1
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);
        }