PrimitiveOperationFormatter.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.Collections.Generic;
  11. using System.Runtime.Serialization;
  12. using System.Reflection;
  13. using System.Xml;
  14. using System.ServiceModel.Diagnostics;
  15. using System.Diagnostics;
  16. using System.Runtime;
  17. class PrimitiveOperationFormatter : IClientMessageFormatter, IDispatchMessageFormatter
  18. {
  19. OperationDescription operation;
  20. MessageDescription responseMessage;
  21. MessageDescription requestMessage;
  22. XmlDictionaryString action;
  23. XmlDictionaryString replyAction;
  24. ActionHeader actionHeaderNone;
  25. ActionHeader actionHeader10;
  26. ActionHeader actionHeaderAugust2004;
  27. ActionHeader replyActionHeaderNone;
  28. ActionHeader replyActionHeader10;
  29. ActionHeader replyActionHeaderAugust2004;
  30. XmlDictionaryString requestWrapperName;
  31. XmlDictionaryString requestWrapperNamespace;
  32. XmlDictionaryString responseWrapperName;
  33. XmlDictionaryString responseWrapperNamespace;
  34. PartInfo[] requestParts;
  35. PartInfo[] responseParts;
  36. PartInfo returnPart;
  37. XmlDictionaryString xsiNilLocalName;
  38. XmlDictionaryString xsiNilNamespace;
  39. public PrimitiveOperationFormatter(OperationDescription description, bool isRpc)
  40. {
  41. if (description == null)
  42. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  43. OperationFormatter.Validate(description, isRpc, false/*isEncoded*/);
  44. this.operation = description;
  45. #pragma warning suppress 56506 // [....], OperationDescription.Messages never be null
  46. this.requestMessage = description.Messages[0];
  47. if (description.Messages.Count == 2)
  48. this.responseMessage = description.Messages[1];
  49. int stringCount = 3 + requestMessage.Body.Parts.Count;
  50. if (responseMessage != null)
  51. stringCount += 2 + responseMessage.Body.Parts.Count;
  52. XmlDictionary dictionary = new XmlDictionary(stringCount * 2);
  53. xsiNilLocalName = dictionary.Add("nil");
  54. xsiNilNamespace = dictionary.Add(System.Xml.Schema.XmlSchema.InstanceNamespace);
  55. OperationFormatter.GetActions(description, dictionary, out this.action, out this.replyAction);
  56. if (requestMessage.Body.WrapperName != null)
  57. {
  58. requestWrapperName = AddToDictionary(dictionary, requestMessage.Body.WrapperName);
  59. requestWrapperNamespace = AddToDictionary(dictionary, requestMessage.Body.WrapperNamespace);
  60. }
  61. requestParts = AddToDictionary(dictionary, requestMessage.Body.Parts, isRpc);
  62. if (responseMessage != null)
  63. {
  64. if (responseMessage.Body.WrapperName != null)
  65. {
  66. responseWrapperName = AddToDictionary(dictionary, responseMessage.Body.WrapperName);
  67. responseWrapperNamespace = AddToDictionary(dictionary, responseMessage.Body.WrapperNamespace);
  68. }
  69. responseParts = AddToDictionary(dictionary, responseMessage.Body.Parts, isRpc);
  70. if (responseMessage.Body.ReturnValue != null && responseMessage.Body.ReturnValue.Type != typeof(void))
  71. {
  72. returnPart = AddToDictionary(dictionary, responseMessage.Body.ReturnValue, isRpc);
  73. }
  74. }
  75. }
  76. ActionHeader ActionHeaderNone
  77. {
  78. get
  79. {
  80. if (actionHeaderNone == null)
  81. {
  82. actionHeaderNone =
  83. ActionHeader.Create(this.action, AddressingVersion.None);
  84. }
  85. return actionHeaderNone;
  86. }
  87. }
  88. ActionHeader ActionHeader10
  89. {
  90. get
  91. {
  92. if (actionHeader10 == null)
  93. {
  94. actionHeader10 =
  95. ActionHeader.Create(this.action, AddressingVersion.WSAddressing10);
  96. }
  97. return actionHeader10;
  98. }
  99. }
  100. ActionHeader ActionHeaderAugust2004
  101. {
  102. get
  103. {
  104. if (actionHeaderAugust2004 == null)
  105. {
  106. actionHeaderAugust2004 =
  107. ActionHeader.Create(this.action, AddressingVersion.WSAddressingAugust2004);
  108. }
  109. return actionHeaderAugust2004;
  110. }
  111. }
  112. ActionHeader ReplyActionHeaderNone
  113. {
  114. get
  115. {
  116. if (replyActionHeaderNone == null)
  117. {
  118. replyActionHeaderNone =
  119. ActionHeader.Create(this.replyAction, AddressingVersion.None);
  120. }
  121. return replyActionHeaderNone;
  122. }
  123. }
  124. ActionHeader ReplyActionHeader10
  125. {
  126. get
  127. {
  128. if (replyActionHeader10 == null)
  129. {
  130. replyActionHeader10 =
  131. ActionHeader.Create(this.replyAction, AddressingVersion.WSAddressing10);
  132. }
  133. return replyActionHeader10;
  134. }
  135. }
  136. ActionHeader ReplyActionHeaderAugust2004
  137. {
  138. get
  139. {
  140. if (replyActionHeaderAugust2004 == null)
  141. {
  142. replyActionHeaderAugust2004 =
  143. ActionHeader.Create(this.replyAction, AddressingVersion.WSAddressingAugust2004);
  144. }
  145. return replyActionHeaderAugust2004;
  146. }
  147. }
  148. static XmlDictionaryString AddToDictionary(XmlDictionary dictionary, string s)
  149. {
  150. XmlDictionaryString dictionaryString;
  151. if (!dictionary.TryLookup(s, out dictionaryString))
  152. {
  153. dictionaryString = dictionary.Add(s);
  154. }
  155. return dictionaryString;
  156. }
  157. static PartInfo[] AddToDictionary(XmlDictionary dictionary, MessagePartDescriptionCollection parts, bool isRpc)
  158. {
  159. PartInfo[] partInfos = new PartInfo[parts.Count];
  160. for (int i = 0; i < parts.Count; i++)
  161. {
  162. partInfos[i] = AddToDictionary(dictionary, parts[i], isRpc);
  163. }
  164. return partInfos;
  165. }
  166. ActionHeader GetActionHeader(AddressingVersion addressing)
  167. {
  168. if (this.action == null)
  169. {
  170. return null;
  171. }
  172. if (addressing == AddressingVersion.WSAddressingAugust2004)
  173. {
  174. return ActionHeaderAugust2004;
  175. }
  176. else if (addressing == AddressingVersion.WSAddressing10)
  177. {
  178. return ActionHeader10;
  179. }
  180. else if (addressing == AddressingVersion.None)
  181. {
  182. return ActionHeaderNone;
  183. }
  184. else
  185. {
  186. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  187. new InvalidOperationException(SR.GetString(SR.AddressingVersionNotSupported, addressing)));
  188. }
  189. }
  190. ActionHeader GetReplyActionHeader(AddressingVersion addressing)
  191. {
  192. if (this.replyAction == null)
  193. {
  194. return null;
  195. }
  196. if (addressing == AddressingVersion.WSAddressingAugust2004)
  197. {
  198. return ReplyActionHeaderAugust2004;
  199. }
  200. else if (addressing == AddressingVersion.WSAddressing10)
  201. {
  202. return ReplyActionHeader10;
  203. }
  204. else if (addressing == AddressingVersion.None)
  205. {
  206. return ReplyActionHeaderNone;
  207. }
  208. else
  209. {
  210. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  211. new InvalidOperationException(SR.GetString(SR.AddressingVersionNotSupported, addressing)));
  212. }
  213. }
  214. static string GetArrayItemName(Type type)
  215. {
  216. switch (Type.GetTypeCode(type))
  217. {
  218. case TypeCode.Boolean:
  219. return "boolean";
  220. case TypeCode.DateTime:
  221. return "dateTime";
  222. case TypeCode.Decimal:
  223. return "decimal";
  224. case TypeCode.Int32:
  225. return "int";
  226. case TypeCode.Int64:
  227. return "long";
  228. case TypeCode.Single:
  229. return "float";
  230. case TypeCode.Double:
  231. return "double";
  232. default:
  233. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidUseOfPrimitiveOperationFormatter)));
  234. }
  235. }
  236. static PartInfo AddToDictionary(XmlDictionary dictionary, MessagePartDescription part, bool isRpc)
  237. {
  238. Type type = part.Type;
  239. XmlDictionaryString itemName = null;
  240. XmlDictionaryString itemNamespace = null;
  241. if (type.IsArray && type != typeof(byte[]))
  242. {
  243. const string ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
  244. string name = GetArrayItemName(type.GetElementType());
  245. itemName = AddToDictionary(dictionary, name);
  246. itemNamespace = AddToDictionary(dictionary, ns);
  247. }
  248. return new PartInfo(part,
  249. AddToDictionary(dictionary, part.Name),
  250. AddToDictionary(dictionary, isRpc ? string.Empty : part.Namespace),
  251. itemName, itemNamespace);
  252. }
  253. public static bool IsContractSupported(OperationDescription description)
  254. {
  255. if (description == null)
  256. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  257. OperationDescription operation = description;
  258. #pragma warning suppress 56506 // [....], OperationDescription.Messages never be null
  259. MessageDescription requestMessage = description.Messages[0];
  260. MessageDescription responseMessage = null;
  261. if (description.Messages.Count == 2)
  262. responseMessage = description.Messages[1];
  263. if (requestMessage.Headers.Count > 0)
  264. return false;
  265. if (requestMessage.Properties.Count > 0)
  266. return false;
  267. if (requestMessage.IsTypedMessage)
  268. return false;
  269. if (responseMessage != null)
  270. {
  271. if (responseMessage.Headers.Count > 0)
  272. return false;
  273. if (responseMessage.Properties.Count > 0)
  274. return false;
  275. if (responseMessage.IsTypedMessage)
  276. return false;
  277. }
  278. if (!AreTypesSupported(requestMessage.Body.Parts))
  279. return false;
  280. if (responseMessage != null)
  281. {
  282. if (!AreTypesSupported(responseMessage.Body.Parts))
  283. return false;
  284. if (responseMessage.Body.ReturnValue != null && !IsTypeSupported(responseMessage.Body.ReturnValue))
  285. return false;
  286. }
  287. return true;
  288. }
  289. static bool AreTypesSupported(MessagePartDescriptionCollection bodyDescriptions)
  290. {
  291. for (int i = 0; i < bodyDescriptions.Count; i++)
  292. if (!IsTypeSupported(bodyDescriptions[i]))
  293. return false;
  294. return true;
  295. }
  296. static bool IsTypeSupported(MessagePartDescription bodyDescription)
  297. {
  298. Fx.Assert(bodyDescription != null, "");
  299. Type type = bodyDescription.Type;
  300. if (type == null)
  301. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxMessagePartDescriptionMissingType, bodyDescription.Name, bodyDescription.Namespace)));
  302. if (bodyDescription.Multiple)
  303. return false;
  304. if (type == typeof(void))
  305. return true;
  306. if (type.IsEnum)
  307. return false;
  308. switch (Type.GetTypeCode(type))
  309. {
  310. case TypeCode.Boolean:
  311. case TypeCode.DateTime:
  312. case TypeCode.Decimal:
  313. case TypeCode.Double:
  314. case TypeCode.Int32:
  315. case TypeCode.Int64:
  316. case TypeCode.Single:
  317. case TypeCode.String:
  318. return true;
  319. case TypeCode.Object:
  320. if (type.IsArray && type.GetArrayRank() == 1 && IsArrayTypeSupported(type.GetElementType()))
  321. return true;
  322. break;
  323. default:
  324. break;
  325. }
  326. return false;
  327. }
  328. static bool IsArrayTypeSupported(Type type)
  329. {
  330. if (type.IsEnum)
  331. return false;
  332. switch (Type.GetTypeCode(type))
  333. {
  334. case TypeCode.Byte:
  335. case TypeCode.Boolean:
  336. case TypeCode.DateTime:
  337. case TypeCode.Decimal:
  338. case TypeCode.Int32:
  339. case TypeCode.Int64:
  340. case TypeCode.Single:
  341. case TypeCode.Double:
  342. return true;
  343. default:
  344. return false;
  345. }
  346. }
  347. public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
  348. {
  349. if (messageVersion == null)
  350. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageVersion");
  351. if (parameters == null)
  352. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  353. return Message.CreateMessage(messageVersion, GetActionHeader(messageVersion.Addressing), new PrimitiveRequestBodyWriter(parameters, this));
  354. }
  355. public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
  356. {
  357. if (messageVersion == null)
  358. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageVersion");
  359. if (parameters == null)
  360. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  361. return Message.CreateMessage(messageVersion, GetReplyActionHeader(messageVersion.Addressing), new PrimitiveResponseBodyWriter(parameters, result, this));
  362. }
  363. public object DeserializeReply(Message message, object[] parameters)
  364. {
  365. if (message == null)
  366. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
  367. if (parameters == null)
  368. throw TraceUtility.ThrowHelperError(new ArgumentNullException("parameters"), message);
  369. try
  370. {
  371. if (message.IsEmpty)
  372. {
  373. if (responseWrapperName == null)
  374. return null;
  375. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.SFxInvalidMessageBodyEmptyMessage)));
  376. }
  377. XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
  378. using (bodyReader)
  379. {
  380. object returnValue = DeserializeResponse(bodyReader, parameters);
  381. message.ReadFromBodyContentsToEnd(bodyReader);
  382. return returnValue;
  383. }
  384. }
  385. catch (XmlException xe)
  386. {
  387. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
  388. SR.GetString(SR.SFxErrorDeserializingReplyBodyMore, operation.Name, xe.Message), xe));
  389. }
  390. catch (FormatException fe)
  391. {
  392. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
  393. SR.GetString(SR.SFxErrorDeserializingReplyBodyMore, operation.Name, fe.Message), fe));
  394. }
  395. catch (SerializationException se)
  396. {
  397. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
  398. SR.GetString(SR.SFxErrorDeserializingReplyBodyMore, operation.Name, se.Message), se));
  399. }
  400. }
  401. public void DeserializeRequest(Message message, object[] parameters)
  402. {
  403. if (message == null)
  404. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
  405. if (parameters == null)
  406. throw TraceUtility.ThrowHelperError(new ArgumentNullException("parameters"), message);
  407. try
  408. {
  409. if (message.IsEmpty)
  410. {
  411. if (requestWrapperName == null)
  412. return;
  413. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.SFxInvalidMessageBodyEmptyMessage)));
  414. }
  415. XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
  416. using (bodyReader)
  417. {
  418. DeserializeRequest(bodyReader, parameters);
  419. message.ReadFromBodyContentsToEnd(bodyReader);
  420. }
  421. }
  422. catch (XmlException xe)
  423. {
  424. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  425. OperationFormatter.CreateDeserializationFailedFault(
  426. SR.GetString(SR.SFxErrorDeserializingRequestBodyMore, operation.Name, xe.Message),
  427. xe));
  428. }
  429. catch (FormatException fe)
  430. {
  431. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  432. OperationFormatter.CreateDeserializationFailedFault(
  433. SR.GetString(SR.SFxErrorDeserializingRequestBodyMore, operation.Name, fe.Message),
  434. fe));
  435. }
  436. catch (SerializationException se)
  437. {
  438. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
  439. SR.GetString(SR.SFxErrorDeserializingRequestBodyMore, operation.Name, se.Message),
  440. se));
  441. }
  442. }
  443. void DeserializeRequest(XmlDictionaryReader reader, object[] parameters)
  444. {
  445. if (requestWrapperName != null)
  446. {
  447. if (!reader.IsStartElement(requestWrapperName, requestWrapperNamespace))
  448. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.SFxInvalidMessageBody, requestWrapperName, requestWrapperNamespace, reader.NodeType, reader.Name, reader.NamespaceURI)));
  449. bool isEmptyElement = reader.IsEmptyElement;
  450. reader.Read();
  451. if (isEmptyElement)
  452. {
  453. return;
  454. }
  455. }
  456. DeserializeParameters(reader, requestParts, parameters);
  457. if (requestWrapperName != null)
  458. {
  459. reader.ReadEndElement();
  460. }
  461. }
  462. object DeserializeResponse(XmlDictionaryReader reader, object[] parameters)
  463. {
  464. if (responseWrapperName != null)
  465. {
  466. if (!reader.IsStartElement(responseWrapperName, responseWrapperNamespace))
  467. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.SFxInvalidMessageBody, responseWrapperName, responseWrapperNamespace, reader.NodeType, reader.Name, reader.NamespaceURI)));
  468. bool isEmptyElement = reader.IsEmptyElement;
  469. reader.Read();
  470. if (isEmptyElement)
  471. {
  472. return null;
  473. }
  474. }
  475. object returnValue = null;
  476. if (returnPart != null)
  477. {
  478. while (true)
  479. {
  480. if (IsPartElement(reader, returnPart))
  481. {
  482. returnValue = DeserializeParameter(reader, returnPart);
  483. break;
  484. }
  485. if (!reader.IsStartElement())
  486. break;
  487. if (IsPartElements(reader, responseParts))
  488. break;
  489. OperationFormatter.TraceAndSkipElement(reader);
  490. }
  491. }
  492. DeserializeParameters(reader, responseParts, parameters);
  493. if (responseWrapperName != null)
  494. {
  495. reader.ReadEndElement();
  496. }
  497. return returnValue;
  498. }
  499. void DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, object[] parameters)
  500. {
  501. if (parts.Length != parameters.Length)
  502. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  503. new ArgumentException(SR.GetString(SR.SFxParameterCountMismatch, "parts", parts.Length, "parameters", parameters.Length), "parameters"));
  504. int nextPartIndex = 0;
  505. while (reader.IsStartElement())
  506. {
  507. for (int i = nextPartIndex; i < parts.Length; i++)
  508. {
  509. PartInfo part = parts[i];
  510. if (IsPartElement(reader, part))
  511. {
  512. parameters[part.Description.Index] = DeserializeParameter(reader, parts[i]);
  513. nextPartIndex = i + 1;
  514. }
  515. else
  516. parameters[part.Description.Index] = null;
  517. }
  518. if (reader.IsStartElement())
  519. OperationFormatter.TraceAndSkipElement(reader);
  520. }
  521. }
  522. private bool IsPartElements(XmlDictionaryReader reader, PartInfo[] parts)
  523. {
  524. foreach (PartInfo part in parts)
  525. if (IsPartElement(reader, part))
  526. return true;
  527. return false;
  528. }
  529. bool IsPartElement(XmlDictionaryReader reader, PartInfo part)
  530. {
  531. return reader.IsStartElement(part.DictionaryName, part.DictionaryNamespace);
  532. }
  533. object DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
  534. {
  535. if (reader.AttributeCount > 0 &&
  536. reader.MoveToAttribute(xsiNilLocalName.Value, xsiNilNamespace.Value) &&
  537. reader.ReadContentAsBoolean())
  538. {
  539. reader.Skip();
  540. return null;
  541. }
  542. return part.ReadValue(reader);
  543. }
  544. void SerializeParameter(XmlDictionaryWriter writer, PartInfo part, object graph)
  545. {
  546. writer.WriteStartElement(part.DictionaryName, part.DictionaryNamespace);
  547. if (graph == null)
  548. {
  549. writer.WriteStartAttribute(xsiNilLocalName, xsiNilNamespace);
  550. writer.WriteValue(true);
  551. writer.WriteEndAttribute();
  552. }
  553. else
  554. part.WriteValue(writer, graph);
  555. writer.WriteEndElement();
  556. }
  557. void SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, object[] parameters)
  558. {
  559. if (parts.Length != parameters.Length)
  560. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  561. new ArgumentException(SR.GetString(SR.SFxParameterCountMismatch, "parts", parts.Length, "parameters", parameters.Length), "parameters"));
  562. for (int i = 0; i < parts.Length; i++)
  563. {
  564. PartInfo part = parts[i];
  565. SerializeParameter(writer, part, parameters[part.Description.Index]);
  566. }
  567. }
  568. void SerializeRequest(XmlDictionaryWriter writer, object[] parameters)
  569. {
  570. if (requestWrapperName != null)
  571. writer.WriteStartElement(requestWrapperName, requestWrapperNamespace);
  572. SerializeParameters(writer, requestParts, parameters);
  573. if (requestWrapperName != null)
  574. writer.WriteEndElement();
  575. }
  576. void SerializeResponse(XmlDictionaryWriter writer, object returnValue, object[] parameters)
  577. {
  578. if (responseWrapperName != null)
  579. writer.WriteStartElement(responseWrapperName, responseWrapperNamespace);
  580. if (returnPart != null)
  581. SerializeParameter(writer, returnPart, returnValue);
  582. SerializeParameters(writer, responseParts, parameters);
  583. if (responseWrapperName != null)
  584. writer.WriteEndElement();
  585. }
  586. class PartInfo
  587. {
  588. XmlDictionaryString dictionaryName;
  589. XmlDictionaryString dictionaryNamespace;
  590. XmlDictionaryString itemName;
  591. XmlDictionaryString itemNamespace;
  592. MessagePartDescription description;
  593. TypeCode typeCode;
  594. bool isArray;
  595. public PartInfo(MessagePartDescription description, XmlDictionaryString dictionaryName, XmlDictionaryString dictionaryNamespace, XmlDictionaryString itemName, XmlDictionaryString itemNamespace)
  596. {
  597. this.dictionaryName = dictionaryName;
  598. this.dictionaryNamespace = dictionaryNamespace;
  599. this.itemName = itemName;
  600. this.itemNamespace = itemNamespace;
  601. this.description = description;
  602. if (description.Type.IsArray)
  603. {
  604. this.isArray = true;
  605. this.typeCode = Type.GetTypeCode(description.Type.GetElementType());
  606. }
  607. else
  608. {
  609. this.isArray = false;
  610. this.typeCode = Type.GetTypeCode(description.Type);
  611. }
  612. }
  613. public MessagePartDescription Description
  614. {
  615. get { return description; }
  616. }
  617. public XmlDictionaryString DictionaryName
  618. {
  619. get { return dictionaryName; }
  620. }
  621. public XmlDictionaryString DictionaryNamespace
  622. {
  623. get { return dictionaryNamespace; }
  624. }
  625. public object ReadValue(XmlDictionaryReader reader)
  626. {
  627. object value;
  628. if (isArray)
  629. {
  630. switch (typeCode)
  631. {
  632. case TypeCode.Byte:
  633. value = reader.ReadElementContentAsBase64();
  634. break;
  635. case TypeCode.Boolean:
  636. if (!reader.IsEmptyElement)
  637. {
  638. reader.ReadStartElement();
  639. value = reader.ReadBooleanArray(itemName, itemNamespace);
  640. reader.ReadEndElement();
  641. }
  642. else
  643. {
  644. reader.Read();
  645. value = new bool[0];
  646. }
  647. break;
  648. case TypeCode.DateTime:
  649. if (!reader.IsEmptyElement)
  650. {
  651. reader.ReadStartElement();
  652. value = reader.ReadDateTimeArray(itemName, itemNamespace);
  653. reader.ReadEndElement();
  654. }
  655. else
  656. {
  657. reader.Read();
  658. value = new DateTime[0];
  659. }
  660. break;
  661. case TypeCode.Decimal:
  662. if (!reader.IsEmptyElement)
  663. {
  664. reader.ReadStartElement();
  665. value = reader.ReadDecimalArray(itemName, itemNamespace);
  666. reader.ReadEndElement();
  667. }
  668. else
  669. {
  670. reader.Read();
  671. value = new Decimal[0];
  672. }
  673. break;
  674. case TypeCode.Int32:
  675. if (!reader.IsEmptyElement)
  676. {
  677. reader.ReadStartElement();
  678. value = reader.ReadInt32Array(itemName, itemNamespace);
  679. reader.ReadEndElement();
  680. }
  681. else
  682. {
  683. reader.Read();
  684. value = new Int32[0];
  685. }
  686. break;
  687. case TypeCode.Int64:
  688. if (!reader.IsEmptyElement)
  689. {
  690. reader.ReadStartElement();
  691. value = reader.ReadInt64Array(itemName, itemNamespace);
  692. reader.ReadEndElement();
  693. }
  694. else
  695. {
  696. reader.Read();
  697. value = new Int64[0];
  698. }
  699. break;
  700. case TypeCode.Single:
  701. if (!reader.IsEmptyElement)
  702. {
  703. reader.ReadStartElement();
  704. value = reader.ReadSingleArray(itemName, itemNamespace);
  705. reader.ReadEndElement();
  706. }
  707. else
  708. {
  709. reader.Read();
  710. value = new Single[0];
  711. }
  712. break;
  713. case TypeCode.Double:
  714. if (!reader.IsEmptyElement)
  715. {
  716. reader.ReadStartElement();
  717. value = reader.ReadDoubleArray(itemName, itemNamespace);
  718. reader.ReadEndElement();
  719. }
  720. else
  721. {
  722. reader.Read();
  723. value = new Double[0];
  724. }
  725. break;
  726. default:
  727. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidUseOfPrimitiveOperationFormatter)));
  728. }
  729. }
  730. else
  731. {
  732. switch (typeCode)
  733. {
  734. case TypeCode.Boolean:
  735. value = reader.ReadElementContentAsBoolean();
  736. break;
  737. case TypeCode.DateTime:
  738. value = reader.ReadElementContentAsDateTime();
  739. break;
  740. case TypeCode.Decimal:
  741. value = reader.ReadElementContentAsDecimal();
  742. break;
  743. case TypeCode.Double:
  744. value = reader.ReadElementContentAsDouble();
  745. break;
  746. case TypeCode.Int32:
  747. value = reader.ReadElementContentAsInt();
  748. break;
  749. case TypeCode.Int64:
  750. value = reader.ReadElementContentAsLong();
  751. break;
  752. case TypeCode.Single:
  753. value = reader.ReadElementContentAsFloat();
  754. break;
  755. case TypeCode.String:
  756. return reader.ReadElementContentAsString();
  757. default:
  758. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidUseOfPrimitiveOperationFormatter)));
  759. }
  760. }
  761. return value;
  762. }
  763. public void WriteValue(XmlDictionaryWriter writer, object value)
  764. {
  765. if (isArray)
  766. {
  767. switch (typeCode)
  768. {
  769. case TypeCode.Byte:
  770. {
  771. byte[] arrayValue = (byte[])value;
  772. writer.WriteBase64(arrayValue, 0, arrayValue.Length);
  773. }
  774. break;
  775. case TypeCode.Boolean:
  776. {
  777. bool[] arrayValue = (bool[])value;
  778. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  779. }
  780. break;
  781. case TypeCode.DateTime:
  782. {
  783. DateTime[] arrayValue = (DateTime[])value;
  784. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  785. }
  786. break;
  787. case TypeCode.Decimal:
  788. {
  789. decimal[] arrayValue = (decimal[])value;
  790. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  791. }
  792. break;
  793. case TypeCode.Int32:
  794. {
  795. Int32[] arrayValue = (Int32[])value;
  796. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  797. }
  798. break;
  799. case TypeCode.Int64:
  800. {
  801. Int64[] arrayValue = (Int64[])value;
  802. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  803. }
  804. break;
  805. case TypeCode.Single:
  806. {
  807. float[] arrayValue = (float[])value;
  808. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  809. }
  810. break;
  811. case TypeCode.Double:
  812. {
  813. double[] arrayValue = (double[])value;
  814. writer.WriteArray(null, itemName, itemNamespace, arrayValue, 0, arrayValue.Length);
  815. }
  816. break;
  817. default:
  818. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidUseOfPrimitiveOperationFormatter)));
  819. }
  820. }
  821. else
  822. {
  823. switch (typeCode)
  824. {
  825. case TypeCode.Boolean:
  826. writer.WriteValue((bool)value);
  827. break;
  828. case TypeCode.DateTime:
  829. writer.WriteValue((DateTime)value);
  830. break;
  831. case TypeCode.Decimal:
  832. writer.WriteValue((Decimal)value);
  833. break;
  834. case TypeCode.Double:
  835. writer.WriteValue((double)value);
  836. break;
  837. case TypeCode.Int32:
  838. writer.WriteValue((int)value);
  839. break;
  840. case TypeCode.Int64:
  841. writer.WriteValue((long)value);
  842. break;
  843. case TypeCode.Single:
  844. writer.WriteValue((float)value);
  845. break;
  846. case TypeCode.String:
  847. writer.WriteString((string)value);
  848. break;
  849. default:
  850. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidUseOfPrimitiveOperationFormatter)));
  851. }
  852. }
  853. }
  854. }
  855. class PrimitiveRequestBodyWriter : BodyWriter
  856. {
  857. object[] parameters;
  858. PrimitiveOperationFormatter primitiveOperationFormatter;
  859. public PrimitiveRequestBodyWriter(object[] parameters, PrimitiveOperationFormatter primitiveOperationFormatter)
  860. : base(true)
  861. {
  862. this.parameters = parameters;
  863. this.primitiveOperationFormatter = primitiveOperationFormatter;
  864. }
  865. protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
  866. {
  867. primitiveOperationFormatter.SerializeRequest(writer, parameters);
  868. }
  869. }
  870. class PrimitiveResponseBodyWriter : BodyWriter
  871. {
  872. object[] parameters;
  873. object returnValue;
  874. PrimitiveOperationFormatter primitiveOperationFormatter;
  875. public PrimitiveResponseBodyWriter(object[] parameters, object returnValue,
  876. PrimitiveOperationFormatter primitiveOperationFormatter)
  877. : base(true)
  878. {
  879. this.parameters = parameters;
  880. this.returnValue = returnValue;
  881. this.primitiveOperationFormatter = primitiveOperationFormatter;
  882. }
  883. protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
  884. {
  885. primitiveOperationFormatter.SerializeResponse(writer, returnValue, parameters);
  886. }
  887. }
  888. }
  889. }