tags:

views:

9

answers:

1

I get the following exception when i modify the incoming Message for a web service method with URItemplate which is not blank.

System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

So it works for

[WebInvoke(Method = "POST", UriTemplate = "/")]

and

[WebInvoke(Method = "PUT", UriTemplate = "/")]

but NOT

[WebInvoke(Method = "PUT", UriTemplate = "/id")]

and NOT

[WebInvoke(Method = "POST", UriTemplate = "/id ")]

Below is how i modify the message.(I sort the xml nodes in alphabetical order)

        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {


            //get the request as xml
            string xml = request.ToString();

            if (!string.IsNullOrWhiteSpace(xml))
            {
                //create a new xml document
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                XElement xmlElement = XElement.Load(new XmlNodeReader(doc));
                XDocument sortedXml = new XDocument();
                XElement root = new XElement(xmlElement.Name);
                sortedXml.Add(root);

                Sort2(xmlElement, root);

                request = Message.CreateMessage(request.Version, null, new SimpleMessageBody(sortedXml.ToString()));
            }
            return null;
        }


 public class SimpleMessageBody : BodyWriter
    {
        string xmlContent;

        public SimpleMessageBody(string content)
            : base(true)
        {
            this.xmlContent = content;
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            using (StringReader stringReader = new StringReader(xmlContent))
            {
                using (XmlReader xmlReader = XmlTextReader.Create(stringReader))
                {
                    writer.WriteNode(xmlReader, true);
                }
            }
        }
    }

I read the following in REstful.net

"You can use Message as a parameter only if it was the only parameter to an operation"

So what are my options..

A: 

changed

 request = Message.CreateMessage(request.Version, null, new SimpleMessageBody(sortedXml.ToString()));
            }

to

Message newMessage = null;
                newMessage = Message.CreateMessage(request.Version, null, new SimpleMessageBody(sortedXml.ToString()));

                newMessage.Headers.CopyHeadersFrom(request);
                newMessage.Properties.CopyProperties(request.Properties);
                request = newMessage;
ps