2 // XmlMessagesFormatter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Eyal Alaluf <eyala@mainsoft.com>
8 // Copyright (C) 2005-2010 Novell, Inc. http://www.novell.com
9 // Copyright (C) 2008 Mainsoft Co. http://www.mainsoft.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.ServiceModel;
36 using System.ServiceModel.Channels;
37 using System.ServiceModel.Description;
40 using System.Xml.Serialization;
42 namespace System.ServiceModel.Dispatcher
44 class XmlMessagesFormatter : BaseMessagesFormatter
46 XmlSerializerFormatAttribute attr;
47 Dictionary<MessageBodyDescription,XmlSerializer> bodySerializers
48 = new Dictionary<MessageBodyDescription,XmlSerializer> ();
50 public XmlMessagesFormatter (OperationDescription desc, XmlSerializerFormatAttribute attr)
56 public XmlMessagesFormatter (MessageDescriptionCollection messages, XmlSerializerFormatAttribute attr)
62 private XmlReflectionMember CreateReflectionMember (MessagePartDescription partDesc, bool isReturnValue)
64 XmlReflectionMember m = new XmlReflectionMember ();
65 m.IsReturnValue = isReturnValue;
66 m.MemberName = partDesc.Name;
67 m.MemberType = partDesc.Type;
68 m.XmlAttributes = new XmlAttributes(partDesc.MemberInfo);
72 protected override Message PartsToMessage (
73 MessageDescription md, MessageVersion version, string action, object [] parts)
75 return Message.CreateMessage (version, action, new XmlBodyWriter (GetSerializer (md.Body), parts));
78 protected override object [] MessageToParts (MessageDescription md, Message message)
83 XmlDictionaryReader r = message.GetReaderAtBodyContents ();
84 return (object []) GetSerializer (md.Body).Deserialize (r);
87 protected override Dictionary<MessageHeaderDescription,object> MessageToHeaderObjects (MessageDescription md, Message message)
89 // FIXME: do we need header serializers?
93 XmlSerializer GetSerializer (MessageBodyDescription desc)
95 if (bodySerializers.ContainsKey (desc))
96 return bodySerializers [desc];
98 int count = desc.Parts.Count + (HasReturnValue (desc) ? 1 : 0);
99 XmlReflectionMember [] members = new XmlReflectionMember [count];
102 if (HasReturnValue (desc))
103 members [ind++] = CreateReflectionMember (desc.ReturnValue, true);
105 foreach (MessagePartDescription partDesc in desc.Parts)
106 members [ind++] = CreateReflectionMember (partDesc, false);
108 XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
109 // Register known types into xmlImporter.
110 foreach (var type in OperationKnownTypes)
111 xmlImporter.IncludeType (type);
112 XmlMembersMapping [] partsMapping = new XmlMembersMapping [1];
113 partsMapping [0] = xmlImporter.ImportMembersMapping (desc.WrapperName, desc.WrapperNamespace, members, true);
114 bodySerializers [desc] = XmlSerializer.FromMappings (partsMapping) [0];
115 return bodySerializers [desc];
118 class XmlBodyWriter : BodyWriter
120 XmlSerializer serializer;
123 public XmlBodyWriter (XmlSerializer serializer, object parts)
126 this.serializer = serializer;
130 protected override BodyWriter OnCreateBufferedCopy (int maxBufferSize)
132 return new XmlBodyWriter (serializer, body);
135 protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
137 serializer.Serialize (writer, body);