DataContractSerializerOperationFormatter.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Runtime;
  9. using System.Runtime.Serialization;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Channels;
  12. using System.ServiceModel.Description;
  13. using System.Xml;
  14. using System.Linq;
  15. static class DataContractSerializerDefaults
  16. {
  17. internal const bool IgnoreExtensionDataObject = false;
  18. internal const int MaxItemsInObjectGraph = int.MaxValue;
  19. internal static DataContractSerializer CreateSerializer(Type type, int maxItems)
  20. {
  21. return CreateSerializer(type, null, maxItems);
  22. }
  23. internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, int maxItems)
  24. {
  25. return new DataContractSerializer(
  26. type,
  27. knownTypes,
  28. maxItems,
  29. DataContractSerializerDefaults.IgnoreExtensionDataObject,
  30. false/*preserveObjectReferences*/,
  31. null/*dataContractSurrage*/);
  32. }
  33. internal static DataContractSerializer CreateSerializer(Type type, string rootName, string rootNs, int maxItems)
  34. {
  35. return CreateSerializer(type, null, rootName, rootNs, maxItems);
  36. }
  37. internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, string rootName, string rootNs, int maxItems)
  38. {
  39. return new DataContractSerializer(
  40. type,
  41. rootName,
  42. rootNs,
  43. knownTypes,
  44. maxItems,
  45. DataContractSerializerDefaults.IgnoreExtensionDataObject,
  46. false/*preserveObjectReferences*/,
  47. null/*dataContractSurrage*/);
  48. }
  49. internal static DataContractSerializer CreateSerializer(Type type, XmlDictionaryString rootName, XmlDictionaryString rootNs, int maxItems)
  50. {
  51. return CreateSerializer(type, null, rootName, rootNs, maxItems);
  52. }
  53. internal static DataContractSerializer CreateSerializer(Type type, IList<Type> knownTypes, XmlDictionaryString rootName, XmlDictionaryString rootNs, int maxItems)
  54. {
  55. return new DataContractSerializer(
  56. type,
  57. rootName,
  58. rootNs,
  59. knownTypes,
  60. maxItems,
  61. DataContractSerializerDefaults.IgnoreExtensionDataObject,
  62. false/*preserveObjectReferences*/,
  63. null/*dataContractSurrage*/);
  64. }
  65. }
  66. class DataContractSerializerOperationFormatter : OperationFormatter
  67. {
  68. static Type typeOfIQueryable = typeof(IQueryable);
  69. static Type typeOfIQueryableGeneric = typeof(IQueryable<>);
  70. static Type typeOfIEnumerable = typeof(IEnumerable);
  71. static Type typeOfIEnumerableGeneric = typeof(IEnumerable<>);
  72. protected MessageInfo requestMessageInfo;
  73. protected MessageInfo replyMessageInfo;
  74. IList<Type> knownTypes;
  75. XsdDataContractExporter dataContractExporter;
  76. DataContractSerializerOperationBehavior serializerFactory;
  77. public DataContractSerializerOperationFormatter(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute,
  78. DataContractSerializerOperationBehavior serializerFactory)
  79. : base(description, dataContractFormatAttribute.Style == OperationFormatStyle.Rpc, false/*isEncoded*/)
  80. {
  81. if (description == null)
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  83. this.serializerFactory = serializerFactory ?? new DataContractSerializerOperationBehavior(description);
  84. foreach (Type type in description.KnownTypes)
  85. {
  86. if (knownTypes == null)
  87. knownTypes = new List<Type>();
  88. if (type == null)
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxKnownTypeNull, description.Name)));
  90. ValidateDataContractType(type);
  91. knownTypes.Add(type);
  92. }
  93. requestMessageInfo = CreateMessageInfo(dataContractFormatAttribute, RequestDescription, this.serializerFactory);
  94. if (ReplyDescription != null)
  95. replyMessageInfo = CreateMessageInfo(dataContractFormatAttribute, ReplyDescription, this.serializerFactory);
  96. }
  97. MessageInfo CreateMessageInfo(DataContractFormatAttribute dataContractFormatAttribute,
  98. MessageDescription messageDescription, DataContractSerializerOperationBehavior serializerFactory)
  99. {
  100. if (messageDescription.IsUntypedMessage)
  101. return null;
  102. MessageInfo messageInfo = new MessageInfo();
  103. MessageBodyDescription body = messageDescription.Body;
  104. if (body.WrapperName != null)
  105. {
  106. messageInfo.WrapperName = AddToDictionary(body.WrapperName);
  107. messageInfo.WrapperNamespace = AddToDictionary(body.WrapperNamespace);
  108. }
  109. MessagePartDescriptionCollection parts = body.Parts;
  110. messageInfo.BodyParts = new PartInfo[parts.Count];
  111. for (int i = 0; i < parts.Count; i++)
  112. messageInfo.BodyParts[i] = CreatePartInfo(parts[i], dataContractFormatAttribute.Style, serializerFactory);
  113. if (IsValidReturnValue(messageDescription.Body.ReturnValue))
  114. messageInfo.ReturnPart = CreatePartInfo(messageDescription.Body.ReturnValue, dataContractFormatAttribute.Style, serializerFactory);
  115. messageInfo.HeaderDescriptionTable = new MessageHeaderDescriptionTable();
  116. messageInfo.HeaderParts = new PartInfo[messageDescription.Headers.Count];
  117. for (int i = 0; i < messageDescription.Headers.Count; i++)
  118. {
  119. MessageHeaderDescription headerDescription = messageDescription.Headers[i];
  120. if (headerDescription.IsUnknownHeaderCollection)
  121. messageInfo.UnknownHeaderDescription = headerDescription;
  122. else
  123. {
  124. ValidateDataContractType(headerDescription.Type);
  125. messageInfo.HeaderDescriptionTable.Add(headerDescription.Name, headerDescription.Namespace, headerDescription);
  126. }
  127. messageInfo.HeaderParts[i] = CreatePartInfo(headerDescription, OperationFormatStyle.Document, serializerFactory);
  128. }
  129. messageInfo.AnyHeaders = messageInfo.UnknownHeaderDescription != null || messageInfo.HeaderDescriptionTable.Count > 0;
  130. return messageInfo;
  131. }
  132. private void ValidateDataContractType(Type type)
  133. {
  134. if (dataContractExporter == null)
  135. {
  136. dataContractExporter = new XsdDataContractExporter();
  137. if (serializerFactory != null && serializerFactory.DataContractSurrogate != null)
  138. {
  139. ExportOptions options = new ExportOptions();
  140. options.DataContractSurrogate = serializerFactory.DataContractSurrogate;
  141. dataContractExporter.Options = options;
  142. }
  143. }
  144. dataContractExporter.GetSchemaTypeName(type); //Throws if the type is not a valid data contract
  145. }
  146. PartInfo CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory)
  147. {
  148. string ns = (style == OperationFormatStyle.Rpc || part.Namespace == null) ? string.Empty : part.Namespace;
  149. PartInfo partInfo = new PartInfo(part, AddToDictionary(part.Name), AddToDictionary(ns), knownTypes, serializerFactory);
  150. ValidateDataContractType(partInfo.ContractType);
  151. return partInfo;
  152. }
  153. protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
  154. {
  155. MessageInfo messageInfo = isRequest ? requestMessageInfo : replyMessageInfo;
  156. PartInfo[] headerParts = messageInfo.HeaderParts;
  157. if (headerParts == null || headerParts.Length == 0)
  158. return;
  159. MessageHeaders headers = message.Headers;
  160. for (int i = 0; i < headerParts.Length; i++)
  161. {
  162. PartInfo headerPart = headerParts[i];
  163. MessageHeaderDescription headerDescription = (MessageHeaderDescription)headerPart.Description;
  164. object headerValue = parameters[headerDescription.Index];
  165. if (headerDescription.Multiple)
  166. {
  167. if (headerValue != null)
  168. {
  169. bool isXmlElement = headerDescription.Type == typeof(XmlElement);
  170. foreach (object headerItemValue in (IEnumerable)headerValue)
  171. AddMessageHeaderForParameter(headers, headerPart, message.Version, headerItemValue, isXmlElement);
  172. }
  173. }
  174. else
  175. AddMessageHeaderForParameter(headers, headerPart, message.Version, headerValue, false/*isXmlElement*/);
  176. }
  177. }
  178. void AddMessageHeaderForParameter(MessageHeaders headers, PartInfo headerPart, MessageVersion messageVersion, object parameterValue, bool isXmlElement)
  179. {
  180. string actor;
  181. bool mustUnderstand;
  182. bool relay;
  183. MessageHeaderDescription headerDescription = (MessageHeaderDescription)headerPart.Description;
  184. object valueToSerialize = GetContentOfMessageHeaderOfT(headerDescription, parameterValue, out mustUnderstand, out relay, out actor);
  185. if (isXmlElement)
  186. {
  187. if (valueToSerialize == null)
  188. return;
  189. XmlElement xmlElement = (XmlElement)valueToSerialize;
  190. headers.Add(new XmlElementMessageHeader(this, messageVersion, xmlElement.LocalName, xmlElement.NamespaceURI, mustUnderstand, actor, relay, xmlElement));
  191. return;
  192. }
  193. headers.Add(new DataContractSerializerMessageHeader(headerPart, valueToSerialize, mustUnderstand, actor, relay));
  194. }
  195. protected override void SerializeBody(XmlDictionaryWriter writer, MessageVersion version, string action, MessageDescription messageDescription, object returnValue, object[] parameters, bool isRequest)
  196. {
  197. if (writer == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));
  198. if (parameters == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters"));
  199. MessageInfo messageInfo;
  200. if (isRequest)
  201. messageInfo = requestMessageInfo;
  202. else
  203. messageInfo = replyMessageInfo;
  204. if (messageInfo.WrapperName != null)
  205. writer.WriteStartElement(messageInfo.WrapperName, messageInfo.WrapperNamespace);
  206. if (messageInfo.ReturnPart != null)
  207. SerializeParameter(writer, messageInfo.ReturnPart, returnValue);
  208. SerializeParameters(writer, messageInfo.BodyParts, parameters);
  209. if (messageInfo.WrapperName != null)
  210. writer.WriteEndElement();
  211. }
  212. void SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, object[] parameters)
  213. {
  214. for (int i = 0; i < parts.Length; i++)
  215. {
  216. PartInfo part = parts[i];
  217. object graph = parameters[part.Description.Index];
  218. SerializeParameter(writer, part, graph);
  219. }
  220. }
  221. void SerializeParameter(XmlDictionaryWriter writer, PartInfo part, object graph)
  222. {
  223. if (part.Description.Multiple)
  224. {
  225. if (graph != null)
  226. {
  227. foreach (object item in (IEnumerable)graph)
  228. SerializeParameterPart(writer, part, item);
  229. }
  230. }
  231. else
  232. SerializeParameterPart(writer, part, graph);
  233. }
  234. void SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, object graph)
  235. {
  236. try
  237. {
  238. part.Serializer.WriteObject(writer, graph);
  239. }
  240. catch (SerializationException sx)
  241. {
  242. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
  243. SR.GetString(SR.SFxInvalidMessageBodyErrorSerializingParameter, part.Description.Namespace, part.Description.Name, sx.Message), sx));
  244. }
  245. }
  246. protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
  247. {
  248. MessageInfo messageInfo = isRequest ? requestMessageInfo : replyMessageInfo;
  249. if (!messageInfo.AnyHeaders)
  250. return;
  251. MessageHeaders headers = message.Headers;
  252. KeyValuePair<Type, ArrayList>[] multipleHeaderValues = null;
  253. ArrayList elementList = null;
  254. if (messageInfo.UnknownHeaderDescription != null)
  255. elementList = new ArrayList();
  256. for (int i = 0; i < headers.Count; i++)
  257. {
  258. MessageHeaderInfo header = headers[i];
  259. MessageHeaderDescription headerDescription = messageInfo.HeaderDescriptionTable.Get(header.Name, header.Namespace);
  260. if (headerDescription != null)
  261. {
  262. if (header.MustUnderstand)
  263. headers.UnderstoodHeaders.Add(header);
  264. object item = null;
  265. XmlDictionaryReader headerReader = headers.GetReaderAtHeader(i);
  266. try
  267. {
  268. object dataValue = DeserializeHeaderContents(headerReader, messageDescription, headerDescription);
  269. if (headerDescription.TypedHeader)
  270. item = TypedHeaderManager.Create(headerDescription.Type, dataValue, headers[i].MustUnderstand, headers[i].Relay, headers[i].Actor);
  271. else
  272. item = dataValue;
  273. }
  274. finally
  275. {
  276. headerReader.Close();
  277. }
  278. if (headerDescription.Multiple)
  279. {
  280. if (multipleHeaderValues == null)
  281. multipleHeaderValues = new KeyValuePair<Type, ArrayList>[parameters.Length];
  282. if (multipleHeaderValues[headerDescription.Index].Key == null)
  283. {
  284. multipleHeaderValues[headerDescription.Index] = new KeyValuePair<System.Type, System.Collections.ArrayList>(headerDescription.TypedHeader ? TypedHeaderManager.GetMessageHeaderType(headerDescription.Type) : headerDescription.Type, new ArrayList());
  285. }
  286. multipleHeaderValues[headerDescription.Index].Value.Add(item);
  287. }
  288. else
  289. parameters[headerDescription.Index] = item;
  290. }
  291. else if (messageInfo.UnknownHeaderDescription != null)
  292. {
  293. MessageHeaderDescription unknownHeaderDescription = messageInfo.UnknownHeaderDescription;
  294. XmlDictionaryReader headerReader = headers.GetReaderAtHeader(i);
  295. try
  296. {
  297. XmlDocument doc = new XmlDocument();
  298. object dataValue = doc.ReadNode(headerReader);
  299. if (dataValue != null && unknownHeaderDescription.TypedHeader)
  300. dataValue = TypedHeaderManager.Create(unknownHeaderDescription.Type, dataValue, headers[i].MustUnderstand, headers[i].Relay, headers[i].Actor);
  301. elementList.Add(dataValue);
  302. }
  303. finally
  304. {
  305. headerReader.Close();
  306. }
  307. }
  308. }
  309. if (multipleHeaderValues != null)
  310. {
  311. for (int i = 0; i < parameters.Length; i++)
  312. {
  313. if (multipleHeaderValues[i].Key != null)
  314. parameters[i] = multipleHeaderValues[i].Value.ToArray(multipleHeaderValues[i].Key);
  315. }
  316. }
  317. if (messageInfo.UnknownHeaderDescription != null)
  318. parameters[messageInfo.UnknownHeaderDescription.Index] = elementList.ToArray(messageInfo.UnknownHeaderDescription.TypedHeader ? typeof(MessageHeader<XmlElement>) : typeof(XmlElement));
  319. }
  320. object DeserializeHeaderContents(XmlDictionaryReader reader, MessageDescription messageDescription, MessageHeaderDescription headerDescription)
  321. {
  322. bool isQueryable;
  323. Type dataContractType = DataContractSerializerOperationFormatter.GetSubstituteDataContractType(headerDescription.Type, out isQueryable);
  324. XmlObjectSerializer serializerLocal = serializerFactory.CreateSerializer(dataContractType, headerDescription.Name, headerDescription.Namespace, this.knownTypes);
  325. object val = serializerLocal.ReadObject(reader);
  326. if (isQueryable && val != null)
  327. {
  328. return Queryable.AsQueryable((IEnumerable)val);
  329. }
  330. return val;
  331. }
  332. protected override object DeserializeBody(XmlDictionaryReader reader, MessageVersion version, string action, MessageDescription messageDescription, object[] parameters, bool isRequest)
  333. {
  334. if (reader == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("reader"));
  335. if (parameters == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters"));
  336. MessageInfo messageInfo;
  337. if (isRequest)
  338. messageInfo = requestMessageInfo;
  339. else
  340. messageInfo = replyMessageInfo;
  341. if (messageInfo.WrapperName != null)
  342. {
  343. if (!reader.IsStartElement(messageInfo.WrapperName, messageInfo.WrapperNamespace))
  344. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.SFxInvalidMessageBody, messageInfo.WrapperName, messageInfo.WrapperNamespace, reader.NodeType, reader.Name, reader.NamespaceURI)));
  345. bool isEmptyElement = reader.IsEmptyElement;
  346. reader.Read();
  347. if (isEmptyElement)
  348. return null;
  349. }
  350. object returnValue = null;
  351. if (messageInfo.ReturnPart != null)
  352. {
  353. while (true)
  354. {
  355. PartInfo part = messageInfo.ReturnPart;
  356. if (part.Serializer.IsStartObject(reader))
  357. {
  358. returnValue = DeserializeParameter(reader, part, isRequest);
  359. break;
  360. }
  361. if (!reader.IsStartElement())
  362. break;
  363. OperationFormatter.TraceAndSkipElement(reader);
  364. }
  365. }
  366. DeserializeParameters(reader, messageInfo.BodyParts, parameters, isRequest);
  367. if (messageInfo.WrapperName != null)
  368. reader.ReadEndElement();
  369. return returnValue;
  370. }
  371. void DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, object[] parameters, bool isRequest)
  372. {
  373. int nextPartIndex = 0;
  374. while (reader.IsStartElement())
  375. {
  376. for (int i = nextPartIndex; i < parts.Length; i++)
  377. {
  378. PartInfo part = parts[i];
  379. if (part.Serializer.IsStartObject(reader))
  380. {
  381. object parameterValue = DeserializeParameter(reader, part, isRequest);
  382. parameters[part.Description.Index] = parameterValue;
  383. nextPartIndex = i + 1;
  384. }
  385. else
  386. parameters[part.Description.Index] = null;
  387. }
  388. if (reader.IsStartElement())
  389. OperationFormatter.TraceAndSkipElement(reader);
  390. }
  391. }
  392. object DeserializeParameter(XmlDictionaryReader reader, PartInfo part, bool isRequest)
  393. {
  394. if (part.Description.Multiple)
  395. {
  396. ArrayList items = new ArrayList();
  397. while (part.Serializer.IsStartObject(reader))
  398. items.Add(DeserializeParameterPart(reader, part, isRequest));
  399. return items.ToArray(part.Description.Type);
  400. }
  401. return DeserializeParameterPart(reader, part, isRequest);
  402. }
  403. object DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, bool isRequest)
  404. {
  405. object val;
  406. try
  407. {
  408. val = part.ReadObject(reader);
  409. }
  410. catch (System.InvalidOperationException e)
  411. {
  412. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  413. SR.GetString(SR.SFxInvalidMessageBodyErrorDeserializingParameter, part.Description.Namespace, part.Description.Name), e));
  414. }
  415. catch (System.Runtime.Serialization.InvalidDataContractException e)
  416. {
  417. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(
  418. SR.GetString(SR.SFxInvalidMessageBodyErrorDeserializingParameter, part.Description.Namespace, part.Description.Name), e));
  419. }
  420. catch (System.FormatException e)
  421. {
  422. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  423. OperationFormatter.CreateDeserializationFailedFault(
  424. SR.GetString(SR.SFxInvalidMessageBodyErrorDeserializingParameterMore,
  425. part.Description.Namespace, part.Description.Name, e.Message),
  426. e));
  427. }
  428. catch (System.Runtime.Serialization.SerializationException e)
  429. {
  430. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  431. OperationFormatter.CreateDeserializationFailedFault(
  432. SR.GetString(SR.SFxInvalidMessageBodyErrorDeserializingParameterMore,
  433. part.Description.Namespace, part.Description.Name, e.Message),
  434. e));
  435. }
  436. return val;
  437. }
  438. internal static Type GetSubstituteDataContractType(Type type, out bool isQueryable)
  439. {
  440. if (type == typeOfIQueryable)
  441. {
  442. isQueryable = true;
  443. return typeOfIEnumerable;
  444. }
  445. if (type.IsGenericType &&
  446. type.GetGenericTypeDefinition() == typeOfIQueryableGeneric)
  447. {
  448. isQueryable = true;
  449. return typeOfIEnumerableGeneric.MakeGenericType(type.GetGenericArguments());
  450. }
  451. isQueryable = false;
  452. return type;
  453. }
  454. class DataContractSerializerMessageHeader : XmlObjectSerializerHeader
  455. {
  456. PartInfo headerPart;
  457. public DataContractSerializerMessageHeader(PartInfo headerPart, object headerValue, bool mustUnderstand, string actor, bool relay)
  458. : base(headerPart.DictionaryName.Value, headerPart.DictionaryNamespace.Value, headerValue, headerPart.Serializer, mustUnderstand, actor ?? string.Empty, relay)
  459. {
  460. this.headerPart = headerPart;
  461. }
  462. protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
  463. {
  464. //Prefix needed since there may be xsi:type attribute at toplevel with qname value where ns = ""
  465. string prefix = (this.Namespace == null || this.Namespace.Length == 0) ? string.Empty : "h";
  466. writer.WriteStartElement(prefix, headerPart.DictionaryName, headerPart.DictionaryNamespace);
  467. WriteHeaderAttributes(writer, messageVersion);
  468. }
  469. }
  470. protected class MessageInfo
  471. {
  472. internal PartInfo[] HeaderParts;
  473. internal XmlDictionaryString WrapperName;
  474. internal XmlDictionaryString WrapperNamespace;
  475. internal PartInfo[] BodyParts;
  476. internal PartInfo ReturnPart;
  477. internal MessageHeaderDescriptionTable HeaderDescriptionTable;
  478. internal MessageHeaderDescription UnknownHeaderDescription;
  479. internal bool AnyHeaders;
  480. }
  481. protected class PartInfo
  482. {
  483. XmlDictionaryString dictionaryName;
  484. XmlDictionaryString dictionaryNamespace;
  485. MessagePartDescription description;
  486. XmlObjectSerializer serializer;
  487. IList<Type> knownTypes;
  488. DataContractSerializerOperationBehavior serializerFactory;
  489. Type contractType;
  490. bool isQueryable;
  491. public PartInfo(MessagePartDescription description, XmlDictionaryString dictionaryName, XmlDictionaryString dictionaryNamespace,
  492. IList<Type> knownTypes, DataContractSerializerOperationBehavior behavior)
  493. {
  494. this.dictionaryName = dictionaryName;
  495. this.dictionaryNamespace = dictionaryNamespace;
  496. this.description = description;
  497. this.knownTypes = knownTypes;
  498. this.serializerFactory = behavior;
  499. this.contractType = DataContractSerializerOperationFormatter.GetSubstituteDataContractType(description.Type, out this.isQueryable);
  500. }
  501. public Type ContractType
  502. {
  503. get { return this.contractType; }
  504. }
  505. public MessagePartDescription Description
  506. {
  507. get { return description; }
  508. }
  509. public XmlDictionaryString DictionaryName
  510. {
  511. get { return dictionaryName; }
  512. }
  513. public XmlDictionaryString DictionaryNamespace
  514. {
  515. get { return dictionaryNamespace; }
  516. }
  517. public XmlObjectSerializer Serializer
  518. {
  519. get
  520. {
  521. if (serializer == null)
  522. {
  523. serializer = serializerFactory.CreateSerializer(contractType, DictionaryName, DictionaryNamespace, knownTypes);
  524. }
  525. return serializer;
  526. }
  527. }
  528. public object ReadObject(XmlDictionaryReader reader)
  529. {
  530. return this.ReadObject(reader, this.Serializer);
  531. }
  532. public object ReadObject(XmlDictionaryReader reader, XmlObjectSerializer serializer)
  533. {
  534. object val = this.serializer.ReadObject(reader, false /* verifyObjectName */);
  535. if (this.isQueryable && val != null)
  536. {
  537. return Queryable.AsQueryable((IEnumerable)val);
  538. }
  539. return val;
  540. }
  541. }
  542. }
  543. }