为具有“非空白”Uri模板的方法修改传入消息时的异常

时间:2010-09-28 15:32:36

标签: .net wcf .net-4.0

当我修改URI服务方法的传入消息时,我得到以下异常,其中URItemplate不是空白。

System.IndexOutOfRangeException:索引超出了数组的范围。在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)处于System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)的System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)at at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)在System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)

所以适用于

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

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

但不是

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

而不是

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

下面是我如何修改消息。(我按字母顺序对xml节点进行排序)

        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);
                }
            }
        }
    }

我在REstful.net

中阅读了以下内容
  

“您可以使用Message作为参数   只有它是唯一的参数   一项行动“

那么我的选择是什么..

1 个答案:

答案 0 :(得分:1)

改变

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

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

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