XmlMessagesFormatter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // XmlMessagesFormatter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Eyal Alaluf <[email protected]>
  7. //
  8. // Copyright (C) 2005-2010 Novell, Inc. http://www.novell.com
  9. // Copyright (C) 2008 Mainsoft Co. http://www.mainsoft.com
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Reflection;
  34. using System.Runtime.Serialization;
  35. using System.ServiceModel;
  36. using System.ServiceModel.Channels;
  37. using System.ServiceModel.Description;
  38. using System.Text;
  39. using System.Xml;
  40. using System.Xml.Serialization;
  41. namespace System.ServiceModel.Dispatcher
  42. {
  43. class XmlMessagesFormatter : BaseMessagesFormatter
  44. {
  45. XmlSerializerFormatAttribute attr;
  46. Dictionary<MessageBodyDescription,XmlSerializer> bodySerializers
  47. = new Dictionary<MessageBodyDescription,XmlSerializer> ();
  48. public XmlMessagesFormatter (OperationDescription desc, XmlSerializerFormatAttribute attr)
  49. : base (desc)
  50. {
  51. this.attr = attr;
  52. }
  53. public XmlMessagesFormatter (MessageDescriptionCollection messages, XmlSerializerFormatAttribute attr)
  54. : base (messages)
  55. {
  56. this.attr = attr;
  57. }
  58. private XmlReflectionMember CreateReflectionMember (MessagePartDescription partDesc, bool isReturnValue)
  59. {
  60. XmlReflectionMember m = new XmlReflectionMember ();
  61. m.IsReturnValue = isReturnValue;
  62. m.MemberName = partDesc.Name;
  63. m.MemberType = partDesc.Type;
  64. m.XmlAttributes = new XmlAttributes(partDesc.MemberInfo);
  65. return m;
  66. }
  67. protected override Message PartsToMessage (
  68. MessageDescription md, MessageVersion version, string action, object [] parts)
  69. {
  70. return Message.CreateMessage (version, action, new XmlBodyWriter (GetSerializer (md.Body), parts));
  71. }
  72. protected override object [] MessageToParts (MessageDescription md, Message message)
  73. {
  74. if (message.IsEmpty)
  75. return null;
  76. XmlDictionaryReader r = message.GetReaderAtBodyContents ();
  77. return (object []) GetSerializer (md.Body).Deserialize (r);
  78. }
  79. protected override Dictionary<MessageHeaderDescription,object> MessageToHeaderObjects (MessageDescription md, Message message)
  80. {
  81. // FIXME: do we need header serializers?
  82. return null;
  83. }
  84. XmlSerializer GetSerializer (MessageBodyDescription desc)
  85. {
  86. if (bodySerializers.ContainsKey (desc))
  87. return bodySerializers [desc];
  88. int count = desc.Parts.Count + (HasReturnValue (desc) ? 1 : 0);
  89. XmlReflectionMember [] members = new XmlReflectionMember [count];
  90. int ind = 0;
  91. if (HasReturnValue (desc))
  92. members [ind++] = CreateReflectionMember (desc.ReturnValue, true);
  93. foreach (MessagePartDescription partDesc in desc.Parts)
  94. members [ind++] = CreateReflectionMember (partDesc, false);
  95. XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
  96. // Register known types into xmlImporter.
  97. foreach (var type in OperationKnownTypes)
  98. xmlImporter.IncludeType (type);
  99. XmlMembersMapping [] partsMapping = new XmlMembersMapping [1];
  100. partsMapping [0] = xmlImporter.ImportMembersMapping (desc.WrapperName, desc.WrapperNamespace, members, true);
  101. bodySerializers [desc] = XmlSerializer.FromMappings (partsMapping) [0];
  102. return bodySerializers [desc];
  103. }
  104. class XmlBodyWriter : BodyWriter
  105. {
  106. XmlSerializer serializer;
  107. object body;
  108. public XmlBodyWriter (XmlSerializer serializer, object parts)
  109. : base (false)
  110. {
  111. this.serializer = serializer;
  112. this.body = parts;
  113. }
  114. protected override BodyWriter OnCreateBufferedCopy (int maxBufferSize)
  115. {
  116. return new XmlBodyWriter (serializer, body);
  117. }
  118. protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
  119. {
  120. serializer.Serialize (writer, body);
  121. }
  122. }
  123. }
  124. }