Merge pull request #1617 from keneanung/OdbcCommandExceptionOnNoData
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Dispatcher / WebMessageFormatter.cs
1 //
2 // WebMessageFormatter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //      Atsushi Enomoto  <atsushi@xamarin.com>
7 //
8 // Copyright (C) 2008,2009 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2011 Xamarin, Inc (http://xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.IO;
34 using System.Reflection;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Json;
37 using System.ServiceModel;
38 using System.ServiceModel.Channels;
39 using System.ServiceModel.Description;
40 using System.ServiceModel.Web;
41 using System.Text;
42 using System.Xml;
43
44 #if NET_2_1
45 using XmlObjectSerializer = System.Object;
46 #endif
47
48 namespace System.ServiceModel.Dispatcher
49 {
50         // This set of classes is to work as message formatters for 
51         // WebHttpBehavior. There are couple of aspects to differentiate
52         // implementations:
53         // - request/reply and client/server
54         //   by WebMessageFormatter hierarchy
55         //   - WebClientMessageFormatter - for client
56         //     - RequestClientFormatter - for request
57         //     - ReplyClientFormatter - for response
58         //   - WebDispatchMessageFormatter - for server
59         //     - RequestDispatchFormatter - for request
60         //     - ReplyDispatchFormatter - for response
61         //
62         // FIXME: below items need more work
63         // - HTTP method differences
64         //  - GET (WebGet)
65         //  - POST (other way)
66         // - output format: Stream, JSON, XML ...
67
68         internal abstract class WebMessageFormatter
69         {
70                 OperationDescription operation;
71                 ServiceEndpoint endpoint;
72                 QueryStringConverter converter;
73                 WebHttpBehavior behavior;
74                 UriTemplate template;
75                 WebAttributeInfo info = null;
76
77                 public WebMessageFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
78                 {
79                         this.operation = operation;
80                         this.endpoint = endpoint;
81                         this.converter = converter;
82                         this.behavior = behavior;
83                         ApplyWebAttribute ();
84 #if !NET_2_1
85                         // This is a hack for WebScriptEnablingBehavior
86                         var jqc = converter as JsonQueryStringConverter;
87                         if (jqc != null)
88                                 BodyName = jqc.CustomWrapperName;
89 #endif
90                 }
91
92                 void ApplyWebAttribute ()
93                 {
94                         MethodInfo mi = operation.SyncMethod ?? operation.BeginMethod;
95
96                         object [] atts = mi.GetCustomAttributes (typeof (WebGetAttribute), false);
97                         if (atts.Length > 0)
98                                 info = ((WebGetAttribute) atts [0]).Info;
99                         atts = mi.GetCustomAttributes (typeof (WebInvokeAttribute), false);
100                         if (atts.Length > 0)
101                                 info = ((WebInvokeAttribute) atts [0]).Info;
102                         if (info == null)
103                                 info = new WebAttributeInfo ();
104
105                         template = info.BuildUriTemplate (Operation, GetMessageDescription (MessageDirection.Input));
106                 }
107
108                 public string BodyName { get; set; }
109
110                 public WebHttpBehavior Behavior {
111                         get { return behavior; }
112                 }
113
114                 public WebAttributeInfo Info {
115                         get { return info; }
116                 }
117
118                 public WebMessageBodyStyle BodyStyle {
119                         get { return info.IsBodyStyleSetExplicitly ? info.BodyStyle : behavior.DefaultBodyStyle; }
120                 }
121
122                 public bool IsRequestBodyWrapped {
123                         get {
124                                 switch (BodyStyle) {
125                                 case WebMessageBodyStyle.Wrapped:
126                                 case WebMessageBodyStyle.WrappedRequest:
127                                         return true;
128                                 }
129                                 return BodyName != null;
130                         }
131                 }
132
133                 public bool IsResponseBodyWrapped {
134                         get {
135                                 switch (BodyStyle) {
136                                 case WebMessageBodyStyle.Wrapped:
137                                 case WebMessageBodyStyle.WrappedResponse:
138                                         return true;
139                                 }
140                                 return BodyName != null;
141                         }
142                 }
143
144                 public OperationDescription Operation {
145                         get { return operation; }
146                 }
147
148                 public QueryStringConverter Converter {
149                         get { return converter; }
150                 }
151
152                 public ServiceEndpoint Endpoint {
153                         get { return endpoint; }
154                 }
155
156                 public UriTemplate UriTemplate {
157                         get { return template; }
158                 }
159
160                 protected WebContentFormat ToContentFormat (WebMessageFormat src, object result)
161                 {
162                         if (result is Stream)
163                                 return WebContentFormat.Raw;
164                         switch (src) {
165                         case WebMessageFormat.Xml:
166                                 return WebContentFormat.Xml;
167                         case WebMessageFormat.Json:
168                                 return WebContentFormat.Json;
169                         }
170                         throw new SystemException ("INTERNAL ERROR: should not happen");
171                 }
172
173                 protected string GetMediaTypeString (WebContentFormat fmt)
174                 {
175                         switch (fmt) {
176                         case WebContentFormat.Raw:
177                                 return "application/octet-stream";
178                         case WebContentFormat.Json:
179                                 return "application/json";
180                         case WebContentFormat.Xml:
181                         default:
182                                 return "application/xml";
183                         }
184                 }
185
186                 protected void CheckMessageVersion (MessageVersion messageVersion)
187                 {
188                         if (messageVersion == null)
189                                 throw new ArgumentNullException ("messageVersion");
190
191                         if (!MessageVersion.None.Equals (messageVersion))
192                                 throw new ArgumentException (String.Format ("Only MessageVersion.None is supported. {0} is not.", messageVersion));
193                 }
194
195                 protected MessageDescription GetMessageDescription (MessageDirection dir)
196                 {
197                         foreach (MessageDescription md in operation.Messages)
198                                 if (md.Direction == dir)
199                                         return md;
200                         throw new SystemException ("INTERNAL ERROR: no corresponding message description for the specified direction: " + dir);
201                 }
202
203                 protected XmlObjectSerializer GetSerializer (WebContentFormat msgfmt, bool isWrapped, MessagePartDescription part)
204                 {
205                         if (part.Type == typeof (void))
206                                 return null; // no serialization should be done.
207
208                         switch (msgfmt) {
209                         case WebContentFormat.Xml:
210                                 if (xml_serializer == null)
211                                         xml_serializer = isWrapped ? new DataContractSerializer (part.Type, part.Name, part.Namespace) : new DataContractSerializer (part.Type);
212                                 return xml_serializer;
213                         case WebContentFormat.Json:
214                                 // FIXME: after name argument they are hack
215                                 if (json_serializer == null)
216                                         json_serializer = isWrapped ? new DataContractJsonSerializer (part.Type, BodyName ?? part.Name, null, 0x100000, false, null, true) : new DataContractJsonSerializer (part.Type);
217                                 return json_serializer;
218                         default:
219                                 throw new NotImplementedException (msgfmt.ToString ());
220                         }
221                 }
222
223                 XmlObjectSerializer xml_serializer, json_serializer;
224
225                 protected object DeserializeObject (XmlObjectSerializer serializer, Message message, MessageDescription md, bool isWrapped, WebContentFormat fmt)
226                 {
227                         // FIXME: handle ref/out parameters
228
229                         var reader = message.GetReaderAtBodyContents ();
230                         reader.MoveToContent ();
231
232                         bool wasEmptyElement = reader.IsEmptyElement;
233
234                         if (isWrapped) {
235                                 if (fmt == WebContentFormat.Json)
236                                         reader.ReadStartElement ("root", String.Empty); // note that the wrapper name is passed to the serializer.
237                                 else
238                                         reader.ReadStartElement (md.Body.WrapperName, md.Body.WrapperNamespace);
239                         }
240
241                         var ret = (serializer == null) ? null : ReadObjectBody (serializer, reader);
242
243                         if (isWrapped && !wasEmptyElement)
244                                 reader.ReadEndElement ();
245
246                         return ret;
247                 }
248                 
249                 protected object ReadObjectBody (XmlObjectSerializer serializer, XmlReader reader)
250                 {
251 #if NET_2_1
252                         return (serializer is DataContractJsonSerializer) ?
253                                 ((DataContractJsonSerializer) serializer).ReadObject (reader) :
254                                 ((DataContractSerializer) serializer).ReadObject (reader, true);
255 #else
256                         return serializer.ReadObject (reader, true);
257 #endif
258                 }
259
260                 internal class RequestClientFormatter : WebClientMessageFormatter
261                 {
262                         public RequestClientFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
263                                 : base (operation, endpoint, converter, behavior)
264                         {
265                         }
266
267                         public override object DeserializeReply (Message message, object [] parameters)
268                         {
269                                 throw new NotSupportedException ();
270                         }
271                 }
272
273                 internal class ReplyClientFormatter : WebClientMessageFormatter
274                 {
275                         public ReplyClientFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
276                                 : base (operation, endpoint, converter, behavior)
277                         {
278                         }
279
280                         public override Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
281                         {
282                                 throw new NotSupportedException ();
283                         }
284                 }
285
286 #if !NET_2_1
287                 internal class RequestDispatchFormatter : WebDispatchMessageFormatter
288                 {
289                         public RequestDispatchFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
290                                 : base (operation, endpoint, converter, behavior)
291                         {
292                         }
293
294                         public override Message SerializeReply (MessageVersion messageVersion, object [] parameters, object result)
295                         {
296                                 throw new NotSupportedException ();
297                         }
298                 }
299
300                 internal class ReplyDispatchFormatter : WebDispatchMessageFormatter
301                 {
302                         public ReplyDispatchFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
303                                 : base (operation, endpoint, converter, behavior)
304                         {
305                         }
306
307                         public override void DeserializeRequest (Message message, object [] parameters)
308                         {
309                                 throw new NotSupportedException ();
310                         }
311                 }
312 #endif
313
314                 internal abstract class WebClientMessageFormatter : WebMessageFormatter, IClientMessageFormatter
315                 {
316                         IClientMessageFormatter default_formatter;
317
318                         protected WebClientMessageFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
319                                 : base (operation, endpoint, converter, behavior)
320                         {
321                         }
322
323                         public virtual Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
324                         {
325                                 if (parameters == null)
326                                         throw new ArgumentNullException ("parameters");
327                                 CheckMessageVersion (messageVersion);
328
329                                 var c = new Dictionary<string,string> ();
330
331                                 MessageDescription md = GetMessageDescription (MessageDirection.Input);
332
333                                 Message ret;
334                                 Uri to;
335                                 object msgpart = null;
336
337
338                                 for (int i = 0; i < parameters.Length; i++) {
339                                         var p = md.Body.Parts [i];
340                                         string name = p.Name.ToUpper (CultureInfo.InvariantCulture);
341                                         if (UriTemplate.PathSegmentVariableNames.Contains (name) ||
342                                             UriTemplate.QueryValueVariableNames.Contains (name))
343                                                 c.Add (name, parameters [i] != null ? Converter.ConvertValueToString (parameters [i], parameters [i].GetType ()) : null);
344                                         else {
345                                                 // FIXME: bind as a message part
346                                                 if (msgpart == null)
347                                                         msgpart = parameters [i];
348                                                 else
349                                                         throw new  NotImplementedException (String.Format ("More than one parameters including {0} that are not contained in the URI template {1} was found.", p.Name, UriTemplate));
350                                         }
351                                 }
352                                 ret = Message.CreateMessage (messageVersion, (string) null, msgpart);
353
354                                 to = UriTemplate.BindByName (Endpoint.Address.Uri, c);
355                                 ret.Headers.To = to;
356
357                                 var hp = new HttpRequestMessageProperty ();
358                                 hp.Method = Info.Method;
359
360                                 WebMessageFormat msgfmt = Info.IsResponseFormatSetExplicitly ? Info.ResponseFormat : Behavior.DefaultOutgoingResponseFormat;
361                                 var contentFormat = ToContentFormat (msgfmt, msgpart);
362                                 string mediaType = GetMediaTypeString (contentFormat);
363                                 // FIXME: get encoding from somewhere
364                                 hp.Headers ["Content-Type"] = mediaType + "; charset=utf-8";
365
366 #if !NET_2_1
367                                 if (WebOperationContext.Current != null)
368                                         WebOperationContext.Current.OutgoingRequest.Apply (hp);
369 #endif
370                                 // FIXME: set hp.SuppressEntityBody for some cases.
371                                 ret.Properties.Add (HttpRequestMessageProperty.Name, hp);
372
373                                 var wp = new WebBodyFormatMessageProperty (ToContentFormat (Info.IsRequestFormatSetExplicitly ? Info.RequestFormat : Behavior.DefaultOutgoingRequestFormat, null));
374                                 ret.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
375
376                                 return ret;
377                         }
378
379                         public virtual object DeserializeReply (Message message, object [] parameters)
380                         {
381                                 if (parameters == null)
382                                         throw new ArgumentNullException ("parameters");
383                                 CheckMessageVersion (message.Version);
384
385 #if !NET_2_1
386                                 if (OperationContext.Current != null) {
387                                         // Set response in the context
388                                         OperationContext.Current.IncomingMessage = message;
389                                 }
390 #endif
391
392                                 if (message.IsEmpty)
393                                         return null; // empty message, could be returned by HttpReplyChannel.
394
395                                 string pname = WebBodyFormatMessageProperty.Name;
396                                 if (!message.Properties.ContainsKey (pname))
397                                         throw new SystemException ("INTERNAL ERROR: it expects WebBodyFormatMessageProperty existence");
398                                 var wp = (WebBodyFormatMessageProperty) message.Properties [pname];
399                                 var fmt = wp != null ? wp.Format : WebContentFormat.Xml;
400
401                                 var md = GetMessageDescription (MessageDirection.Output);
402                                 var serializer = GetSerializer (wp.Format, IsResponseBodyWrapped, md.Body.ReturnValue);
403                                 var ret = DeserializeObject (serializer, message, md, IsResponseBodyWrapped, fmt);
404
405                                 return ret;
406                         }
407                 }
408
409                 internal class WrappedBodyWriter : BodyWriter
410                 {
411                         public WrappedBodyWriter (object value, XmlObjectSerializer serializer, string name, string ns, WebContentFormat fmt)
412                                 : base (true)
413                         {
414                                 this.name = name;
415                                 this.ns = ns;
416                                 this.value = value;
417                                 this.serializer = serializer;
418                                 this.fmt = fmt;
419                         }
420
421                         WebContentFormat fmt;
422                         string name, ns;
423                         object value;
424                         XmlObjectSerializer serializer;
425
426 #if !NET_2_1
427                         protected override BodyWriter OnCreateBufferedCopy (int maxBufferSize)
428                         {
429                                 return new WrappedBodyWriter (value, serializer, name, ns, fmt);
430                         }
431 #endif
432
433                         protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
434                         {
435                                 switch (fmt) {
436                                 case WebContentFormat.Raw:
437                                         WriteRawContents (writer);
438                                         break;
439                                 case WebContentFormat.Json:
440                                         WriteJsonBodyContents (writer);
441                                         break;
442                                 case WebContentFormat.Xml:
443                                         WriteXmlBodyContents (writer);
444                                         break;
445                                 }
446                         }
447                         
448                         void WriteRawContents (XmlDictionaryWriter writer)
449                         {
450                                 throw new NotSupportedException ("Some unsupported sequence of writing operation occured. It is likely a missing feature.");
451                         }
452                         
453                         void WriteJsonBodyContents (XmlDictionaryWriter writer)
454                         {
455                                 if (name != null) {
456                                         writer.WriteStartElement ("root");
457                                         writer.WriteAttributeString ("type", "object");
458                                 }
459                                 WriteObject (serializer, writer, value);
460                                 if (name != null)
461                                         writer.WriteEndElement ();
462                         }
463
464                         void WriteXmlBodyContents (XmlDictionaryWriter writer)
465                         {
466                                 if (name != null)
467                                         writer.WriteStartElement (name, ns);
468                                 WriteObject (serializer, writer, value);
469                                 if (name != null)
470                                         writer.WriteEndElement ();
471                         }
472
473                         void WriteObject (XmlObjectSerializer serializer, XmlDictionaryWriter writer, object value)
474                         {
475                                 if (serializer != null){
476 #if NET_2_1
477                                         if (serializer is DataContractJsonSerializer)
478                                                 ((DataContractJsonSerializer) serializer).WriteObject (writer, value);
479                                         else
480                                                 ((DataContractSerializer) serializer).WriteObject (writer, value);
481 #else
482                                         serializer.WriteObject (writer, value);
483 #endif
484                                 }
485                         }
486                 }
487
488 #if !NET_2_1
489                 internal abstract class WebDispatchMessageFormatter : WebMessageFormatter, IDispatchMessageFormatter
490                 {
491                         protected WebDispatchMessageFormatter (OperationDescription operation, ServiceEndpoint endpoint, QueryStringConverter converter, WebHttpBehavior behavior)
492                                 : base (operation, endpoint, converter, behavior)
493                         {
494                         }
495
496                         public virtual Message SerializeReply (MessageVersion messageVersion, object [] parameters, object result)
497                         {
498                                 try {
499                                         return SerializeReplyCore (messageVersion, parameters, result);
500                                 } finally {
501                                         if (WebOperationContext.Current != null)
502                                                 OperationContext.Current.Extensions.Remove (WebOperationContext.Current);
503                                 }
504                         }
505
506                         Message SerializeReplyCore (MessageVersion messageVersion, object [] parameters, object result)
507                         {
508                                 // parameters could be null.
509                                 // result could be null. For Raw output, it becomes no output.
510
511                                 CheckMessageVersion (messageVersion);
512
513                                 MessageDescription md = GetMessageDescription (MessageDirection.Output);
514
515                                 // FIXME: use them.
516                                 // var dcob = Operation.Behaviors.Find<DataContractSerializerOperationBehavior> ();
517                                 // XmlObjectSerializer xos = dcob.CreateSerializer (result.GetType (), md.Body.WrapperName, md.Body.WrapperNamespace, null);
518                                 // var xsob = Operation.Behaviors.Find<XmlSerializerOperationBehavior> ();
519                                 // XmlSerializer [] serializers = XmlSerializer.FromMappings (xsob.GetXmlMappings ().ToArray ());
520
521                                 WebMessageFormat msgfmt = Info.IsResponseFormatSetExplicitly ? Info.ResponseFormat : Behavior.DefaultOutgoingResponseFormat;
522
523                                 XmlObjectSerializer serializer = null;
524
525                                 // FIXME: serialize ref/out parameters as well.
526
527                                 string name = null, ns = null;
528
529                                 switch (msgfmt) {
530                                 case WebMessageFormat.Xml:
531                                         serializer = GetSerializer (WebContentFormat.Xml, IsResponseBodyWrapped, md.Body.ReturnValue);
532                                         name = IsResponseBodyWrapped ? md.Body.WrapperName : null;
533                                         ns = IsResponseBodyWrapped ? md.Body.WrapperNamespace : null;
534                                         break;
535                                 case WebMessageFormat.Json:
536                                         serializer = GetSerializer (WebContentFormat.Json, IsResponseBodyWrapped, md.Body.ReturnValue);
537                                         name = IsResponseBodyWrapped ? (BodyName ?? md.Body.ReturnValue.Name) : null;
538                                         ns = String.Empty;
539                                         break;
540                                 }
541
542                                 var contentFormat = ToContentFormat (msgfmt, result);
543                                 string mediaType = GetMediaTypeString (contentFormat);
544                                 Message ret = contentFormat == WebContentFormat.Raw ? new RawMessage ((Stream) result) : Message.CreateMessage (MessageVersion.None, null, new WrappedBodyWriter (result, serializer, name, ns, contentFormat));
545
546                                 // Message properties
547
548                                 var hp = new HttpResponseMessageProperty ();
549                                 // FIXME: get encoding from somewhere
550                                 hp.Headers ["Content-Type"] = mediaType + "; charset=utf-8";
551
552                                 // apply user-customized HTTP results via WebOperationContext.
553                                 if (WebOperationContext.Current != null) // this formatter must be available outside ServiceHost.
554                                         WebOperationContext.Current.OutgoingResponse.Apply (hp);
555
556                                 // FIXME: fill some properties if required.
557                                 ret.Properties.Add (HttpResponseMessageProperty.Name, hp);
558
559                                 var wp = new WebBodyFormatMessageProperty (contentFormat);
560                                 ret.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
561
562                                 return ret;
563                         }
564
565                         public virtual void DeserializeRequest (Message message, object [] parameters)
566                         {
567                                 if (parameters == null)
568                                         throw new ArgumentNullException ("parameters");
569                                 CheckMessageVersion (message.Version);
570
571                                 IncomingWebRequestContext iwc = null;
572                                 if (OperationContext.Current != null) {
573                                         OperationContext.Current.Extensions.Add (new WebOperationContext (OperationContext.Current));
574                                         iwc = WebOperationContext.Current.IncomingRequest;
575                                 }
576                                 
577                                 var wp = message.Properties [WebBodyFormatMessageProperty.Name] as WebBodyFormatMessageProperty;
578                                 var fmt = wp != null ? wp.Format : WebContentFormat.Xml;
579
580                                 Uri to = message.Headers.To;
581                                 UriTemplateMatch match = to == null ? null : UriTemplate.Match (Endpoint.Address.Uri, to);
582                                 if (match != null && iwc != null)
583                                         iwc.UriTemplateMatch = match;
584
585                                 MessageDescription md = GetMessageDescription (MessageDirection.Input);
586
587                                 for (int i = 0; i < parameters.Length; i++) {
588                                         var p = md.Body.Parts [i];
589                                         string name = p.Name.ToUpperInvariant ();
590                                         if (fmt == WebContentFormat.Raw && p.Type == typeof (Stream)) {
591                                                 var rmsg = (RawMessage) message;
592                                                 parameters [i] = rmsg.Stream;
593                                         } else {
594                                                 var str = match.BoundVariables [name];
595                                                 if (str != null)
596                                                         parameters [i] = Converter.ConvertStringToValue (str, p.Type);
597                                                 else {
598                                                         if (info.Method != "GET") {
599                                                                 var serializer = GetSerializer (fmt, IsRequestBodyWrapped, p);
600                                                                 parameters [i] = DeserializeObject (serializer, message, md, IsRequestBodyWrapped, fmt);
601                                                         }
602                                                         // for GET Uri template parameters, there is no <anyType xsi:nil='true' />. So just skip the member.
603                                                 }
604                                         }
605                                 }
606                         }
607                 }
608 #endif
609
610                 internal class RawMessage : Message
611                 {
612                         public RawMessage (Stream stream)
613                         {
614                                 this.Stream = stream;
615                                 headers = new MessageHeaders (MessageVersion.None);
616                                 properties = new MessageProperties ();
617                         }
618                 
619                         public override MessageVersion Version {
620                                 get { return MessageVersion.None; }
621                         }
622                 
623                         MessageHeaders headers;
624
625                         public override MessageHeaders Headers {
626                                 get { return headers; }
627                         }
628                 
629                         MessageProperties properties;
630
631                         public override MessageProperties Properties {
632                                 get { return properties; }
633                         }
634
635                         public Stream Stream { get; private set; }
636
637                         protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
638                         {
639                                 writer.WriteString ("-- message body is raw binary --");
640                         }
641
642                         protected override MessageBuffer OnCreateBufferedCopy (int maxBufferSize)
643                         {
644                                 var ms = Stream as MemoryStream;
645                                 if (ms == null) {
646                                         ms = new MemoryStream ();
647                                         Stream.CopyTo (ms);
648                                         this.Stream = ms;
649                                 }
650                                 return new RawMessageBuffer (ms.ToArray (), headers, properties);
651                         }
652                 }
653                 
654                 internal class RawMessageBuffer : MessageBuffer
655                 {
656                         byte [] buffer;
657                         MessageHeaders headers;
658                         MessageProperties properties;
659
660                         public RawMessageBuffer (byte [] buffer, MessageHeaders headers, MessageProperties properties)
661                         {
662                                 this.buffer = buffer;
663                                 this.headers = new MessageHeaders (headers);
664                                 this.properties = new MessageProperties (properties);
665                         }
666                         
667                         public override int BufferSize {
668                                 get { return buffer.Length; }
669                         }
670                         
671                         public override void Close ()
672                         {
673                         }
674                         
675                         public override Message CreateMessage ()
676                         {
677                                 var msg = new RawMessage (new MemoryStream (buffer));
678                                 msg.Headers.CopyHeadersFrom (headers);
679                                 msg.Properties.CopyProperties (properties);
680                                 return msg;
681                         }
682                 }
683         }
684 }