| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Runtime.Serialization; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Runtime.Serialization; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Dispatcher |
| | | 16 | | { |
| | | 17 | | internal static class DataContractSerializerDefaults |
| | | 18 | | { |
| | | 19 | | internal const bool IgnoreExtensionDataObject = false; |
| | | 20 | | internal const int MaxItemsInObjectGraph = int.MaxValue; |
| | | 21 | | |
| | | 22 | | internal static DataContractSerializer CreateSerializer(Type type, int maxItems) |
| | | 23 | | { |
| | | 24 | | return CreateSerializer(type, null, maxItems); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, int maxItems) |
| | | 28 | | { |
| | | 29 | | return new DataContractSerializer( |
| | | 30 | | type, |
| | | 31 | | knownTypes); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static DataContractSerializer CreateSerializer(Type type, string rootName, string rootNs, int maxItems) |
| | | 35 | | { |
| | | 36 | | return CreateSerializer(type, null, rootName, rootNs, maxItems); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, string rootName, stri |
| | | 40 | | { |
| | | 41 | | XmlDictionary dictionary = new XmlDictionary(2); |
| | | 42 | | return new DataContractSerializer( |
| | | 43 | | type, |
| | | 44 | | dictionary.Add(rootName), |
| | | 45 | | dictionary.Add(rootNs), |
| | | 46 | | knownTypes); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | internal static DataContractSerializer CreateSerializer(Type type, XmlDictionaryString rootName, XmlDictionarySt |
| | | 50 | | { |
| | | 51 | | return CreateSerializer(type, null, rootName, rootNs, maxItems); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, XmlDictionaryString r |
| | | 55 | | { |
| | | 56 | | return new DataContractSerializer( |
| | | 57 | | type, |
| | | 58 | | rootName, |
| | | 59 | | rootNs, |
| | | 60 | | knownTypes); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public class DataContractSerializerOperationFormatter : OperationFormatter |
| | | 65 | | { |
| | 4 | 66 | | private static readonly Type s_typeOfIQueryable = typeof(IQueryable); |
| | 4 | 67 | | private static readonly Type s_typeOfIQueryableGeneric = typeof(IQueryable<>); |
| | 4 | 68 | | private static readonly Type s_typeOfIEnumerable = typeof(IEnumerable); |
| | 4 | 69 | | private static readonly Type s_typeOfIEnumerableGeneric = typeof(IEnumerable<>); |
| | | 70 | | |
| | | 71 | | protected MessageInfo requestMessageInfo; |
| | | 72 | | protected MessageInfo replyMessageInfo; |
| | | 73 | | private readonly IList<Type> _knownTypes; |
| | | 74 | | |
| | | 75 | | private IXsdDataContractExporter _dataContractExporter; |
| | | 76 | | private readonly DataContractSerializerOperationBehavior _serializerFactory; |
| | | 77 | | |
| | | 78 | | public DataContractSerializerOperationFormatter(OperationDescription description, DataContractFormatAttribute da |
| | | 79 | | DataContractSerializerOperationBehavior serializerFactory) |
| | 817 | 80 | | : base(description, dataContractFormatAttribute.Style == OperationFormatStyle.Rpc, false/*isEncoded*/) |
| | | 81 | | { |
| | 817 | 82 | | if (description == null) |
| | | 83 | | { |
| | 0 | 84 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | | 85 | | } |
| | | 86 | | |
| | 817 | 87 | | _serializerFactory = serializerFactory ?? new DataContractSerializerOperationBehavior(description); |
| | 1816 | 88 | | foreach (Type type in description.KnownTypes) |
| | | 89 | | { |
| | 91 | 90 | | if (_knownTypes == null) |
| | | 91 | | { |
| | 88 | 92 | | _knownTypes = new List<Type>(); |
| | | 93 | | } |
| | | 94 | | |
| | 91 | 95 | | if (type == null) |
| | | 96 | | { |
| | 0 | 97 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 98 | | } |
| | | 99 | | |
| | 91 | 100 | | ValidateDataContractType(type); |
| | 91 | 101 | | _knownTypes.Add(type); |
| | | 102 | | } |
| | 817 | 103 | | requestMessageInfo = CreateMessageInfo(dataContractFormatAttribute, RequestDescription, _serializerFactory); |
| | 817 | 104 | | if (ReplyDescription != null) |
| | | 105 | | { |
| | 813 | 106 | | replyMessageInfo = CreateMessageInfo(dataContractFormatAttribute, ReplyDescription, _serializerFactory); |
| | | 107 | | } |
| | 817 | 108 | | } |
| | | 109 | | |
| | | 110 | | private MessageInfo CreateMessageInfo(DataContractFormatAttribute dataContractFormatAttribute, |
| | | 111 | | MessageDescription messageDescription, DataContractSerializerOperationBehavior serializerFactory) |
| | | 112 | | { |
| | 1630 | 113 | | if (messageDescription.IsUntypedMessage) |
| | | 114 | | { |
| | 16 | 115 | | return null; |
| | | 116 | | } |
| | | 117 | | |
| | 1614 | 118 | | MessageInfo messageInfo = new MessageInfo(); |
| | | 119 | | |
| | 1614 | 120 | | MessageBodyDescription body = messageDescription.Body; |
| | 1614 | 121 | | if (body.WrapperName != null) |
| | | 122 | | { |
| | 1598 | 123 | | messageInfo.WrapperName = AddToDictionary(body.WrapperName); |
| | 1598 | 124 | | messageInfo.WrapperNamespace = AddToDictionary(body.WrapperNamespace); |
| | | 125 | | } |
| | 1614 | 126 | | MessagePartDescriptionCollection parts = body.Parts; |
| | 1614 | 127 | | messageInfo.BodyParts = new PartInfo[parts.Count]; |
| | 5516 | 128 | | for (int i = 0; i < parts.Count; i++) |
| | | 129 | | { |
| | 1144 | 130 | | messageInfo.BodyParts[i] = CreatePartInfo(parts[i], dataContractFormatAttribute.Style, serializerFactory |
| | | 131 | | } |
| | | 132 | | |
| | 1614 | 133 | | if (IsValidReturnValue(messageDescription.Body.ReturnValue)) |
| | | 134 | | { |
| | 548 | 135 | | messageInfo.ReturnPart = CreatePartInfo(messageDescription.Body.ReturnValue, dataContractFormatAttribute |
| | | 136 | | } |
| | | 137 | | |
| | 1614 | 138 | | messageInfo.HeaderDescriptionTable = new MessageHeaderDescriptionTable(); |
| | 1614 | 139 | | messageInfo.HeaderParts = new PartInfo[messageDescription.Headers.Count]; |
| | 3600 | 140 | | for (int i = 0; i < messageDescription.Headers.Count; i++) |
| | | 141 | | { |
| | 186 | 142 | | MessageHeaderDescription headerDescription = messageDescription.Headers[i]; |
| | 186 | 143 | | if (headerDescription.IsUnknownHeaderCollection) |
| | | 144 | | { |
| | 0 | 145 | | messageInfo.UnknownHeaderDescription = headerDescription; |
| | | 146 | | } |
| | | 147 | | else |
| | | 148 | | { |
| | 186 | 149 | | ValidateDataContractType(headerDescription.Type); |
| | 186 | 150 | | messageInfo.HeaderDescriptionTable.Add(headerDescription.Name, headerDescription.Namespace, headerDe |
| | | 151 | | } |
| | 186 | 152 | | messageInfo.HeaderParts[i] = CreatePartInfo(headerDescription, OperationFormatStyle.Document, serializer |
| | | 153 | | } |
| | 1614 | 154 | | messageInfo.AnyHeaders = messageInfo.UnknownHeaderDescription != null || messageInfo.HeaderDescriptionTable. |
| | 1614 | 155 | | return messageInfo; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private void ValidateDataContractType(Type type) |
| | | 159 | | { |
| | 2155 | 160 | | if (_dataContractExporter == null) |
| | | 161 | | { |
| | 801 | 162 | | _dataContractExporter = XsdDataContractExporterFactory.Create(); |
| | | 163 | | //if (_serializerFactory != null && _serializerFactory.DataContractSurrogate != null) |
| | | 164 | | //{ |
| | | 165 | | // ExportOptions options = new ExportOptions(); |
| | | 166 | | // options.DataContractSurrogate = serializerFactory.DataContractSurrogate; |
| | | 167 | | // dataContractExporter.Options = options; |
| | | 168 | | //} |
| | | 169 | | } |
| | 2155 | 170 | | _dataContractExporter.GetSchemaTypeName(type); //Throws if the type is not a valid data contract |
| | 2155 | 171 | | } |
| | | 172 | | |
| | | 173 | | private PartInfo CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerO |
| | | 174 | | { |
| | 1878 | 175 | | string ns = (style == OperationFormatStyle.Rpc || part.Namespace == null) ? string.Empty : part.Namespace; |
| | 1878 | 176 | | PartInfo partInfo = new PartInfo(part, AddToDictionary(part.Name), AddToDictionary(ns), _knownTypes, seriali |
| | 1878 | 177 | | ValidateDataContractType(partInfo.ContractType); |
| | 1878 | 178 | | return partInfo; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] par |
| | | 182 | | { |
| | 146 | 183 | | MessageInfo messageInfo = isRequest ? requestMessageInfo : replyMessageInfo; |
| | 146 | 184 | | PartInfo[] headerParts = messageInfo.HeaderParts; |
| | 146 | 185 | | if (headerParts == null || headerParts.Length == 0) |
| | | 186 | | { |
| | 139 | 187 | | return; |
| | | 188 | | } |
| | | 189 | | |
| | 7 | 190 | | MessageHeaders headers = message.Headers; |
| | 30 | 191 | | for (int i = 0; i < headerParts.Length; i++) |
| | | 192 | | { |
| | 8 | 193 | | PartInfo headerPart = headerParts[i]; |
| | 8 | 194 | | MessageHeaderDescription headerDescription = (MessageHeaderDescription)headerPart.Description; |
| | 8 | 195 | | object headerValue = parameters[headerDescription.Index]; |
| | | 196 | | |
| | 8 | 197 | | if (headerDescription.Multiple) |
| | | 198 | | { |
| | 0 | 199 | | if (headerValue != null) |
| | | 200 | | { |
| | 0 | 201 | | bool isXmlElement = headerDescription.Type == typeof(XmlElement); |
| | 0 | 202 | | foreach (object headerItemValue in (IEnumerable)headerValue) |
| | | 203 | | { |
| | 0 | 204 | | AddMessageHeaderForParameter(headers, headerPart, message.Version, headerItemValue, isXmlEle |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | else |
| | | 209 | | { |
| | 8 | 210 | | AddMessageHeaderForParameter(headers, headerPart, message.Version, headerValue, false/*isXmlElement* |
| | | 211 | | } |
| | | 212 | | } |
| | 7 | 213 | | } |
| | | 214 | | |
| | | 215 | | private void AddMessageHeaderForParameter(MessageHeaders headers, PartInfo headerPart, MessageVersion messageVer |
| | | 216 | | { |
| | 8 | 217 | | MessageHeaderDescription headerDescription = (MessageHeaderDescription)headerPart.Description; |
| | 8 | 218 | | object valueToSerialize = GetContentOfMessageHeaderOfT(headerDescription, parameterValue, out bool mustUnder |
| | | 219 | | |
| | 8 | 220 | | if (isXmlElement) |
| | | 221 | | { |
| | 0 | 222 | | if (valueToSerialize == null) |
| | | 223 | | { |
| | 0 | 224 | | return; |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | XmlElement xmlElement = (XmlElement)valueToSerialize; |
| | 0 | 228 | | headers.Add(new XmlElementMessageHeader(this, messageVersion, xmlElement.LocalName, xmlElement.Namespace |
| | 0 | 229 | | return; |
| | | 230 | | } |
| | 8 | 231 | | headers.Add(new DataContractSerializerMessageHeader(headerPart, valueToSerialize, mustUnderstand, actor, rel |
| | 8 | 232 | | } |
| | | 233 | | |
| | | 234 | | protected override void SerializeBody(XmlDictionaryWriter writer, MessageVersion version, string action, Message |
| | | 235 | | { |
| | 135 | 236 | | if (writer == null) |
| | | 237 | | { |
| | 0 | 238 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(writer))); |
| | | 239 | | } |
| | | 240 | | |
| | 135 | 241 | | if (parameters == null) |
| | | 242 | | { |
| | 0 | 243 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters))) |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | MessageInfo messageInfo; |
| | 135 | 247 | | if (isRequest) |
| | | 248 | | { |
| | 0 | 249 | | messageInfo = requestMessageInfo; |
| | | 250 | | } |
| | | 251 | | else |
| | | 252 | | { |
| | 135 | 253 | | messageInfo = replyMessageInfo; |
| | | 254 | | } |
| | | 255 | | |
| | 135 | 256 | | if (messageInfo.WrapperName != null) |
| | | 257 | | { |
| | 133 | 258 | | writer.WriteStartElement(messageInfo.WrapperName, messageInfo.WrapperNamespace); |
| | | 259 | | } |
| | | 260 | | |
| | 135 | 261 | | if (messageInfo.ReturnPart != null) |
| | | 262 | | { |
| | 99 | 263 | | SerializeParameter(writer, messageInfo.ReturnPart, returnValue); |
| | | 264 | | } |
| | | 265 | | |
| | 134 | 266 | | SerializeParameters(writer, messageInfo.BodyParts, parameters); |
| | 134 | 267 | | if (messageInfo.WrapperName != null) |
| | | 268 | | { |
| | 132 | 269 | | writer.WriteEndElement(); |
| | | 270 | | } |
| | 134 | 271 | | } |
| | | 272 | | |
| | | 273 | | private void SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, object[] parameters) |
| | | 274 | | { |
| | 390 | 275 | | for (int i = 0; i < parts.Length; i++) |
| | | 276 | | { |
| | 61 | 277 | | PartInfo part = parts[i]; |
| | 61 | 278 | | object graph = parameters[part.Description.Index]; |
| | 61 | 279 | | SerializeParameter(writer, part, graph); |
| | | 280 | | } |
| | 134 | 281 | | } |
| | | 282 | | |
| | | 283 | | private void SerializeParameter(XmlDictionaryWriter writer, PartInfo part, object graph) |
| | | 284 | | { |
| | 160 | 285 | | if (part.Description.Multiple) |
| | | 286 | | { |
| | 0 | 287 | | if (graph != null) |
| | | 288 | | { |
| | 0 | 289 | | foreach (object item in (IEnumerable)graph) |
| | | 290 | | { |
| | 0 | 291 | | SerializeParameterPart(writer, part, item); |
| | | 292 | | } |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | else |
| | | 296 | | { |
| | 160 | 297 | | SerializeParameterPart(writer, part, graph); |
| | | 298 | | } |
| | 159 | 299 | | } |
| | | 300 | | |
| | | 301 | | private void SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, object graph) |
| | | 302 | | { |
| | | 303 | | try |
| | | 304 | | { |
| | 160 | 305 | | part.Serializer.WriteObject(writer, graph); |
| | 159 | 306 | | } |
| | 1 | 307 | | catch (SerializationException sx) |
| | | 308 | | { |
| | 1 | 309 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 1 | 310 | | SR.Format(SR.SFxInvalidMessageBodyErrorSerializingParameter, part.Description.Namespace, part.Descri |
| | | 311 | | } |
| | 159 | 312 | | } |
| | | 313 | | |
| | | 314 | | protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] p |
| | | 315 | | { |
| | 154 | 316 | | MessageInfo messageInfo = isRequest ? requestMessageInfo : replyMessageInfo; |
| | 154 | 317 | | if (!messageInfo.AnyHeaders) |
| | | 318 | | { |
| | 146 | 319 | | return; |
| | | 320 | | } |
| | | 321 | | |
| | 8 | 322 | | MessageHeaders headers = message.Headers; |
| | 8 | 323 | | KeyValuePair<Type, ArrayList>[] multipleHeaderValues = null; |
| | 8 | 324 | | ArrayList elementList = null; |
| | 8 | 325 | | if (messageInfo.UnknownHeaderDescription != null) |
| | | 326 | | { |
| | 0 | 327 | | elementList = new ArrayList(); |
| | | 328 | | } |
| | | 329 | | |
| | 72 | 330 | | for (int i = 0; i < headers.Count; i++) |
| | | 331 | | { |
| | 28 | 332 | | MessageHeaderInfo header = headers[i]; |
| | 28 | 333 | | MessageHeaderDescription headerDescription = messageInfo.HeaderDescriptionTable.Get(header.Name, header. |
| | 28 | 334 | | if (headerDescription != null) |
| | | 335 | | { |
| | 8 | 336 | | if (header.MustUnderstand) |
| | | 337 | | { |
| | 0 | 338 | | headers.UnderstoodHeaders.Add(header); |
| | | 339 | | } |
| | | 340 | | |
| | 8 | 341 | | object item = null; |
| | 8 | 342 | | XmlDictionaryReader headerReader = headers.GetReaderAtHeader(i); |
| | | 343 | | try |
| | | 344 | | { |
| | 8 | 345 | | object dataValue = DeserializeHeaderContents(headerReader, messageDescription, headerDescription |
| | 8 | 346 | | if (headerDescription.TypedHeader) |
| | | 347 | | { |
| | 0 | 348 | | item = TypedHeaderManager.Create(headerDescription.Type, dataValue, headers[i].MustUnderstan |
| | | 349 | | } |
| | | 350 | | else |
| | | 351 | | { |
| | 8 | 352 | | item = dataValue; |
| | | 353 | | } |
| | 8 | 354 | | } |
| | | 355 | | finally |
| | | 356 | | { |
| | 8 | 357 | | headerReader.Dispose(); |
| | 8 | 358 | | } |
| | | 359 | | |
| | 8 | 360 | | if (headerDescription.Multiple) |
| | | 361 | | { |
| | 0 | 362 | | if (multipleHeaderValues == null) |
| | | 363 | | { |
| | 0 | 364 | | multipleHeaderValues = new KeyValuePair<Type, ArrayList>[parameters.Length]; |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (multipleHeaderValues[headerDescription.Index].Key == null) |
| | | 368 | | { |
| | 0 | 369 | | multipleHeaderValues[headerDescription.Index] = new KeyValuePair<System.Type, System.Collect |
| | | 370 | | } |
| | 0 | 371 | | multipleHeaderValues[headerDescription.Index].Value.Add(item); |
| | | 372 | | } |
| | | 373 | | else |
| | | 374 | | { |
| | 8 | 375 | | parameters[headerDescription.Index] = item; |
| | | 376 | | } |
| | | 377 | | } |
| | 20 | 378 | | else if (messageInfo.UnknownHeaderDescription != null) |
| | | 379 | | { |
| | 0 | 380 | | MessageHeaderDescription unknownHeaderDescription = messageInfo.UnknownHeaderDescription; |
| | 0 | 381 | | XmlDictionaryReader headerReader = headers.GetReaderAtHeader(i); |
| | | 382 | | try |
| | | 383 | | { |
| | 0 | 384 | | XmlDocument doc = new XmlDocument(); |
| | 0 | 385 | | object dataValue = doc.ReadNode(headerReader); |
| | 0 | 386 | | if (dataValue != null && unknownHeaderDescription.TypedHeader) |
| | | 387 | | { |
| | 0 | 388 | | dataValue = TypedHeaderManager.Create(unknownHeaderDescription.Type, dataValue, headers[i].M |
| | | 389 | | } |
| | | 390 | | |
| | 0 | 391 | | elementList.Add(dataValue); |
| | 0 | 392 | | } |
| | | 393 | | finally |
| | | 394 | | { |
| | 0 | 395 | | headerReader.Dispose(); |
| | 0 | 396 | | } |
| | | 397 | | } |
| | | 398 | | } |
| | 8 | 399 | | if (multipleHeaderValues != null) |
| | | 400 | | { |
| | 0 | 401 | | for (int i = 0; i < parameters.Length; i++) |
| | | 402 | | { |
| | 0 | 403 | | if (multipleHeaderValues[i].Key != null) |
| | | 404 | | { |
| | 0 | 405 | | parameters[i] = multipleHeaderValues[i].Value.ToArray(multipleHeaderValues[i].Key); |
| | | 406 | | } |
| | | 407 | | } |
| | | 408 | | } |
| | 8 | 409 | | if (messageInfo.UnknownHeaderDescription != null) |
| | | 410 | | { |
| | 0 | 411 | | parameters[messageInfo.UnknownHeaderDescription.Index] = elementList.ToArray(messageInfo.UnknownHeaderDe |
| | | 412 | | } |
| | 8 | 413 | | } |
| | | 414 | | |
| | | 415 | | private object DeserializeHeaderContents(XmlDictionaryReader reader, MessageDescription messageDescription, Mess |
| | | 416 | | { |
| | 8 | 417 | | Type dataContractType = GetSubstituteDataContractType(headerDescription.Type, out bool isQueryable); |
| | 8 | 418 | | XmlObjectSerializer serializerLocal = _serializerFactory.CreateSerializer(dataContractType, headerDescriptio |
| | 8 | 419 | | object val = serializerLocal.ReadObject(reader); |
| | 8 | 420 | | if (isQueryable && val != null) |
| | | 421 | | { |
| | 0 | 422 | | return Queryable.AsQueryable((IEnumerable)val); |
| | | 423 | | } |
| | 8 | 424 | | return val; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | protected override object DeserializeBody(XmlDictionaryReader reader, MessageVersion version, string action, Mes |
| | | 428 | | { |
| | 137 | 429 | | if (reader == null) |
| | | 430 | | { |
| | 0 | 431 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(reader))); |
| | | 432 | | } |
| | | 433 | | |
| | 137 | 434 | | if (parameters == null) |
| | | 435 | | { |
| | 0 | 436 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters))) |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | MessageInfo messageInfo; |
| | 137 | 440 | | if (isRequest) |
| | | 441 | | { |
| | 137 | 442 | | messageInfo = requestMessageInfo; |
| | | 443 | | } |
| | | 444 | | else |
| | | 445 | | { |
| | 0 | 446 | | messageInfo = replyMessageInfo; |
| | | 447 | | } |
| | | 448 | | |
| | 137 | 449 | | if (messageInfo.WrapperName != null) |
| | | 450 | | { |
| | 137 | 451 | | if (!reader.IsStartElement(messageInfo.WrapperName, messageInfo.WrapperNamespace)) |
| | | 452 | | { |
| | 0 | 453 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.SF |
| | | 454 | | } |
| | | 455 | | |
| | 137 | 456 | | bool isEmptyElement = reader.IsEmptyElement; |
| | 137 | 457 | | reader.Read(); |
| | 137 | 458 | | if (isEmptyElement) |
| | | 459 | | { |
| | 21 | 460 | | return null; |
| | | 461 | | } |
| | | 462 | | } |
| | 116 | 463 | | object returnValue = null; |
| | 116 | 464 | | if (messageInfo.ReturnPart != null) |
| | | 465 | | { |
| | 0 | 466 | | while (true) |
| | | 467 | | { |
| | 0 | 468 | | PartInfo part = messageInfo.ReturnPart; |
| | 0 | 469 | | if (part.Serializer.IsStartObject(reader)) |
| | | 470 | | { |
| | 0 | 471 | | returnValue = DeserializeParameter(reader, part, isRequest); |
| | 0 | 472 | | break; |
| | | 473 | | } |
| | 0 | 474 | | if (!reader.IsStartElement()) |
| | | 475 | | { |
| | | 476 | | break; |
| | | 477 | | } |
| | | 478 | | |
| | 0 | 479 | | TraceAndSkipElement(reader); |
| | | 480 | | } |
| | | 481 | | } |
| | 116 | 482 | | DeserializeParameters(reader, messageInfo.BodyParts, parameters, isRequest); |
| | 116 | 483 | | if (messageInfo.WrapperName != null) |
| | | 484 | | { |
| | 116 | 485 | | reader.ReadEndElement(); |
| | | 486 | | } |
| | | 487 | | |
| | 116 | 488 | | return returnValue; |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | private void DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, object[] parameters, bool isReq |
| | | 492 | | { |
| | 116 | 493 | | int nextPartIndex = 0; |
| | 226 | 494 | | while (reader.IsStartElement()) |
| | | 495 | | { |
| | 444 | 496 | | for (int i = nextPartIndex; i < parts.Length; i++) |
| | | 497 | | { |
| | 112 | 498 | | PartInfo part = parts[i]; |
| | 112 | 499 | | if (part.Serializer.IsStartObject(reader)) |
| | | 500 | | { |
| | 112 | 501 | | object parameterValue = DeserializeParameter(reader, part, isRequest); |
| | 112 | 502 | | parameters[part.Description.Index] = parameterValue; |
| | 112 | 503 | | nextPartIndex = i + 1; |
| | | 504 | | } |
| | | 505 | | else |
| | | 506 | | { |
| | 0 | 507 | | parameters[part.Description.Index] = null; |
| | | 508 | | } |
| | | 509 | | } |
| | | 510 | | |
| | 110 | 511 | | if (reader.IsStartElement()) |
| | | 512 | | { |
| | 0 | 513 | | TraceAndSkipElement(reader); |
| | | 514 | | } |
| | | 515 | | } |
| | 116 | 516 | | } |
| | | 517 | | |
| | | 518 | | private object DeserializeParameter(XmlDictionaryReader reader, PartInfo part, bool isRequest) |
| | | 519 | | { |
| | 112 | 520 | | if (part.Description.Multiple) |
| | | 521 | | { |
| | 0 | 522 | | ArrayList items = new ArrayList(); |
| | 0 | 523 | | while (part.Serializer.IsStartObject(reader)) |
| | | 524 | | { |
| | 0 | 525 | | items.Add(DeserializeParameterPart(reader, part, isRequest)); |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | return items.ToArray(part.Description.Type); |
| | | 529 | | } |
| | 112 | 530 | | return DeserializeParameterPart(reader, part, isRequest); |
| | | 531 | | } |
| | | 532 | | |
| | | 533 | | private object DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, bool isRequest) |
| | | 534 | | { |
| | | 535 | | object val; |
| | | 536 | | try |
| | | 537 | | { |
| | 112 | 538 | | val = part.ReadObject(reader); |
| | 112 | 539 | | } |
| | 0 | 540 | | catch (System.InvalidOperationException e) |
| | | 541 | | { |
| | 0 | 542 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 543 | | SR.Format(SR.SFxInvalidMessageBodyErrorDeserializingParameter, part.Description.Namespace, part.Desc |
| | | 544 | | } |
| | 0 | 545 | | catch (System.Runtime.Serialization.InvalidDataContractException e) |
| | | 546 | | { |
| | 0 | 547 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException( |
| | 0 | 548 | | SR.Format(SR.SFxInvalidMessageBodyErrorDeserializingParameter, part.Description.Namespace, part.Desc |
| | | 549 | | } |
| | 0 | 550 | | catch (System.FormatException e) |
| | | 551 | | { |
| | 0 | 552 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 553 | | NetDispatcherFaultException.CreateDeserializationFailedFault( |
| | 0 | 554 | | SR.Format(SR.SFxInvalidMessageBodyErrorDeserializingParameterMore, part.Description.Namespace, p |
| | 0 | 555 | | e)); |
| | | 556 | | } |
| | 0 | 557 | | catch (System.Runtime.Serialization.SerializationException e) |
| | | 558 | | { |
| | 0 | 559 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 560 | | NetDispatcherFaultException.CreateDeserializationFailedFault( |
| | 0 | 561 | | SR.Format(SR.SFxInvalidMessageBodyErrorDeserializingParameterMore, part.Description.Namespace, p |
| | 0 | 562 | | e)); |
| | | 563 | | } |
| | | 564 | | |
| | 112 | 565 | | return val; |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | internal static Type GetSubstituteDataContractType(Type type, out bool isQueryable) |
| | | 569 | | { |
| | 2045 | 570 | | if (type == s_typeOfIQueryable) |
| | | 571 | | { |
| | 0 | 572 | | isQueryable = true; |
| | 0 | 573 | | return s_typeOfIEnumerable; |
| | | 574 | | } |
| | | 575 | | |
| | 2045 | 576 | | if (type.GetTypeInfo().IsGenericType && |
| | 2045 | 577 | | type.GetGenericTypeDefinition() == s_typeOfIQueryableGeneric) |
| | | 578 | | { |
| | 0 | 579 | | isQueryable = true; |
| | 0 | 580 | | return s_typeOfIEnumerableGeneric.MakeGenericType(type.GetGenericArguments()); |
| | | 581 | | } |
| | | 582 | | |
| | 2045 | 583 | | isQueryable = false; |
| | 2045 | 584 | | return type; |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | private class DataContractSerializerMessageHeader : XmlObjectSerializerHeader |
| | | 588 | | { |
| | | 589 | | private readonly PartInfo _headerPart; |
| | | 590 | | |
| | | 591 | | public DataContractSerializerMessageHeader(PartInfo headerPart, object headerValue, bool mustUnderstand, str |
| | 8 | 592 | | : base(headerPart.DictionaryName.Value, headerPart.DictionaryNamespace.Value, headerValue, headerPart.Se |
| | | 593 | | { |
| | 8 | 594 | | _headerPart = headerPart; |
| | 8 | 595 | | } |
| | | 596 | | |
| | | 597 | | protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 598 | | { |
| | | 599 | | //Prefix needed since there may be xsi:type attribute at toplevel with qname value where ns = "" |
| | 8 | 600 | | string prefix = (Namespace == null || Namespace.Length == 0) ? string.Empty : "h"; |
| | 8 | 601 | | writer.WriteStartElement(prefix, _headerPart.DictionaryName, _headerPart.DictionaryNamespace); |
| | 8 | 602 | | WriteHeaderAttributes(writer, messageVersion); |
| | 8 | 603 | | } |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | |
| | | 607 | | protected class MessageInfo |
| | | 608 | | { |
| | | 609 | | internal PartInfo[] HeaderParts; |
| | | 610 | | public XmlDictionaryString WrapperName; |
| | | 611 | | public XmlDictionaryString WrapperNamespace; |
| | | 612 | | public PartInfo[] BodyParts; |
| | | 613 | | public PartInfo ReturnPart; |
| | | 614 | | internal MessageHeaderDescriptionTable HeaderDescriptionTable; |
| | | 615 | | internal MessageHeaderDescription UnknownHeaderDescription; |
| | | 616 | | internal bool AnyHeaders; |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | protected class PartInfo |
| | | 620 | | { |
| | | 621 | | private XmlObjectSerializer _serializer; |
| | | 622 | | private readonly IList<Type> _knownTypes; |
| | | 623 | | private readonly DataContractSerializerOperationBehavior _serializerFactory; |
| | | 624 | | private readonly bool _isQueryable; |
| | | 625 | | |
| | | 626 | | public PartInfo(MessagePartDescription description, XmlDictionaryString dictionaryName, XmlDictionaryString |
| | | 627 | | IList<Type> knownTypes, DataContractSerializerOperationBehavior behavior) |
| | 1880 | 628 | | { |
| | 1880 | 629 | | DictionaryName = dictionaryName; |
| | | 630 | | DictionaryNamespace = dictionaryNamespace; |
| | 1880 | 631 | | Description = description; |
| | 1880 | 632 | | _knownTypes = knownTypes; |
| | 1880 | 633 | | _serializerFactory = behavior; |
| | 1880 | 634 | | |
| | 1880 | 635 | | ContractType = GetSubstituteDataContractType(description.Type, out _isQueryable); |
| | | 636 | | } |
| | 1880 | 637 | | |
| | 1880 | 638 | | public Type ContractType { get; } |
| | | 639 | | |
| | 2131 | 640 | | public MessagePartDescription Description { get; } |
| | | 641 | | |
| | 463 | 642 | | public XmlDictionaryString DictionaryName { get; } |
| | | 643 | | |
| | 269 | 644 | | public XmlDictionaryString DictionaryNamespace { get; } |
| | | 645 | | |
| | 269 | 646 | | public XmlObjectSerializer Serializer |
| | | 647 | | { |
| | | 648 | | get |
| | | 649 | | { |
| | | 650 | | if (_serializer == null) |
| | | 651 | | { |
| | 394 | 652 | | _serializer = _serializerFactory.CreateSerializer(ContractType, DictionaryName, DictionaryNamesp |
| | | 653 | | } |
| | 253 | 654 | | return _serializer; |
| | | 655 | | } |
| | 394 | 656 | | } |
| | | 657 | | |
| | | 658 | | public object ReadObject(XmlDictionaryReader reader) |
| | | 659 | | { |
| | | 660 | | return ReadObject(reader, Serializer); |
| | | 661 | | } |
| | | 662 | | |
| | | 663 | | public object ReadObject(XmlDictionaryReader reader, XmlObjectSerializer serializer) |
| | | 664 | | { |
| | | 665 | | object val = _serializer.ReadObject(reader, false /* verifyObjectName */); |
| | | 666 | | if (_isQueryable && val != null) |
| | | 667 | | { |
| | | 668 | | return Queryable.AsQueryable((IEnumerable)val); |
| | | 669 | | } |
| | | 670 | | return val; |
| | 388 | 671 | | } |
| | | 672 | | } |
| | 244 | 673 | | } |
| | 244 | 674 | | } |