SoapProtocolReflector.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // System.Web.Services.Description.SoapProtocolReflector.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  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.Web.Services;
  31. using System.Web.Services.Protocols;
  32. using System.Xml.Serialization;
  33. using System.Xml.Schema;
  34. using System.Xml;
  35. namespace System.Web.Services.Description {
  36. internal class SoapProtocolReflector : ProtocolReflector
  37. {
  38. #region Fields
  39. internal const string EncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  40. SoapBinding soapBinding;
  41. #endregion // Fields
  42. #region Constructors
  43. public SoapProtocolReflector ()
  44. {
  45. }
  46. #endregion // Constructors
  47. #region Properties
  48. public override string ProtocolName {
  49. get { return "Soap"; }
  50. }
  51. #endregion // Properties
  52. #region Methods
  53. protected override void BeginClass ()
  54. {
  55. SoapBinding sb = new SoapBinding ();
  56. sb.Transport = SoapBinding.HttpTransport;
  57. sb.Style = ((SoapTypeStubInfo)TypeInfo).SoapBindingStyle;
  58. Binding.Extensions.Add (sb);
  59. SoapAddressBinding abind = new SoapAddressBinding ();
  60. abind.Location = ServiceUrl;
  61. Port.Extensions.Add (abind);
  62. }
  63. protected override void EndClass ()
  64. {
  65. }
  66. protected override bool ReflectMethod ()
  67. {
  68. SoapOperationBinding sob = new SoapOperationBinding();
  69. SoapMethodStubInfo method = (SoapMethodStubInfo) MethodStubInfo;
  70. sob.SoapAction = method.Action;
  71. sob.Style = method.SoapBindingStyle;
  72. OperationBinding.Extensions.Add (sob);
  73. ImportMessage (method.InputMembersMapping, InputMessage);
  74. ImportMessage (method.OutputMembersMapping, OutputMessage);
  75. AddOperationMsgBindings (method, OperationBinding.Input);
  76. AddOperationMsgBindings (method, OperationBinding.Output);
  77. foreach (HeaderInfo hf in method.Headers)
  78. {
  79. if (hf.IsUnknownHeader) continue;
  80. Message msg = new Message ();
  81. msg.Name = Operation.Name + hf.HeaderType.Name;
  82. MessagePart part = new MessagePart ();
  83. part.Name = hf.HeaderType.Name;
  84. msg.Parts.Add (part);
  85. ServiceDescription.Messages.Add (msg);
  86. SoapHeaderBinding hb = new SoapHeaderBinding ();
  87. hb.Message = new XmlQualifiedName (msg.Name, ServiceDescription.TargetNamespace);
  88. hb.Part = part.Name;
  89. hb.Use = method.Use;
  90. if (method.Use == SoapBindingUse.Literal)
  91. {
  92. // MS.NET reflects header classes in a weird way. The root element
  93. // name is the CLR class name unless it is specified in an XmlRootAttribute.
  94. // The usual is to use the xml type name by default, but not in this case.
  95. XmlRootAttribute root;
  96. XmlAttributes ats = new XmlAttributes (hf.HeaderType);
  97. if (ats.XmlRoot != null) root = ats.XmlRoot;
  98. else root = new XmlRootAttribute (hf.HeaderType.Name);
  99. if (root.Namespace == null) root.Namespace = TypeInfo.LogicalType.GetWebServiceLiteralNamespace (ServiceDescription.TargetNamespace);
  100. if (root.ElementName == null) root.ElementName = hf.HeaderType.Name;
  101. XmlTypeMapping mapping = ReflectionImporter.ImportTypeMapping (hf.HeaderType, root);
  102. part.Element = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  103. SchemaExporter.ExportTypeMapping (mapping);
  104. }
  105. else
  106. {
  107. XmlTypeMapping mapping = SoapReflectionImporter.ImportTypeMapping (hf.HeaderType, TypeInfo.LogicalType.GetWebServiceEncodedNamespace (ServiceDescription.TargetNamespace));
  108. part.Type = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  109. SoapSchemaExporter.ExportTypeMapping (mapping);
  110. hb.Encoding = EncodingNamespace;
  111. }
  112. if ((hf.Direction & SoapHeaderDirection.Out) != 0)
  113. OperationBinding.Output.Extensions.Add (hb);
  114. if ((hf.Direction & SoapHeaderDirection.In) != 0)
  115. OperationBinding.Input.Extensions.Add (hb);
  116. }
  117. return true;
  118. }
  119. void AddOperationMsgBindings (SoapMethodStubInfo method, MessageBinding msg)
  120. {
  121. SoapBodyBinding sbbo = new SoapBodyBinding();
  122. msg.Extensions.Add (sbbo);
  123. sbbo.Use = method.Use;
  124. if (method.Use == SoapBindingUse.Encoded)
  125. {
  126. sbbo.Namespace = ServiceDescription.TargetNamespace;
  127. sbbo.Encoding = EncodingNamespace;
  128. }
  129. }
  130. void ImportMessage (XmlMembersMapping members, Message msg)
  131. {
  132. SoapMethodStubInfo method = (SoapMethodStubInfo) MethodStubInfo;
  133. bool needsEnclosingElement = (method.ParameterStyle == SoapParameterStyle.Wrapped &&
  134. method.SoapBindingStyle == SoapBindingStyle.Document);
  135. if (needsEnclosingElement)
  136. {
  137. MessagePart part = new MessagePart ();
  138. part.Name = "parameters";
  139. XmlQualifiedName qname = new XmlQualifiedName (members.ElementName, members.Namespace);
  140. if (method.Use == SoapBindingUse.Literal) part.Element = qname;
  141. else part.Type = qname;
  142. msg.Parts.Add (part);
  143. }
  144. else
  145. {
  146. for (int n=0; n<members.Count; n++)
  147. {
  148. MessagePart part = new MessagePart ();
  149. part.Name = members[n].MemberName;
  150. if (method.Use == SoapBindingUse.Literal) {
  151. if (members[n].Any)
  152. part.Type = new XmlQualifiedName ("any", members[n].Namespace);
  153. else
  154. part.Element = new XmlQualifiedName (members[n].ElementName, members[n].Namespace);
  155. }
  156. else {
  157. string namesp = members[n].TypeNamespace;
  158. if (namesp == "") namesp = members[n].Namespace;
  159. part.Type = new XmlQualifiedName (members[n].TypeName, namesp);
  160. }
  161. msg.Parts.Add (part);
  162. }
  163. }
  164. if (method.Use == SoapBindingUse.Literal)
  165. SchemaExporter.ExportMembersMapping (members);
  166. else
  167. SoapSchemaExporter.ExportMembersMapping (members, needsEnclosingElement);
  168. }
  169. protected override string ReflectMethodBinding ()
  170. {
  171. return ((SoapMethodStubInfo)MethodStubInfo).Binding;
  172. }
  173. #endregion
  174. }
  175. }