SoapProtocolReflector.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Message msg = new Message ();
  80. msg.Name = Operation.Name + hf.HeaderType.Name;
  81. MessagePart part = new MessagePart ();
  82. part.Name = hf.HeaderType.Name;
  83. msg.Parts.Add (part);
  84. ServiceDescription.Messages.Add (msg);
  85. SoapHeaderBinding hb = new SoapHeaderBinding ();
  86. hb.Message = new XmlQualifiedName (msg.Name, ServiceDescription.TargetNamespace);
  87. hb.Part = part.Name;
  88. hb.Use = method.Use;
  89. if (method.Use == SoapBindingUse.Literal)
  90. {
  91. // MS.NET reflects header classes in a weird way. The root element
  92. // name is the CLR class name unless it is specified in an XmlRootAttribute.
  93. // The usual is to use the xml type name by default, but not in this case.
  94. XmlRootAttribute root;
  95. XmlAttributes ats = new XmlAttributes (hf.HeaderType);
  96. if (ats.XmlRoot != null) root = ats.XmlRoot;
  97. else root = new XmlRootAttribute (hf.HeaderType.Name);
  98. if (root.Namespace == null) root.Namespace = TypeInfo.LogicalType.GetWebServiceLiteralNamespace (ServiceDescription.TargetNamespace);
  99. if (root.ElementName == null) root.ElementName = hf.HeaderType.Name;
  100. XmlTypeMapping mapping = ReflectionImporter.ImportTypeMapping (hf.HeaderType, root);
  101. part.Element = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  102. SchemaExporter.ExportTypeMapping (mapping);
  103. }
  104. else
  105. {
  106. XmlTypeMapping mapping = SoapReflectionImporter.ImportTypeMapping (hf.HeaderType, TypeInfo.LogicalType.GetWebServiceEncodedNamespace (ServiceDescription.TargetNamespace));
  107. part.Type = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  108. SoapSchemaExporter.ExportTypeMapping (mapping);
  109. hb.Encoding = EncodingNamespace;
  110. }
  111. if ((hf.Direction & SoapHeaderDirection.Out) != 0)
  112. OperationBinding.Output.Extensions.Add (hb);
  113. if ((hf.Direction & SoapHeaderDirection.In) != 0)
  114. OperationBinding.Input.Extensions.Add (hb);
  115. }
  116. return true;
  117. }
  118. void AddOperationMsgBindings (SoapMethodStubInfo method, MessageBinding msg)
  119. {
  120. SoapBodyBinding sbbo = new SoapBodyBinding();
  121. msg.Extensions.Add (sbbo);
  122. sbbo.Use = method.Use;
  123. if (method.Use == SoapBindingUse.Encoded)
  124. {
  125. sbbo.Namespace = ServiceDescription.TargetNamespace;
  126. sbbo.Encoding = EncodingNamespace;
  127. }
  128. }
  129. void ImportMessage (XmlMembersMapping members, Message msg)
  130. {
  131. SoapMethodStubInfo method = (SoapMethodStubInfo) MethodStubInfo;
  132. bool needsEnclosingElement = (method.ParameterStyle == SoapParameterStyle.Wrapped &&
  133. method.SoapBindingStyle == SoapBindingStyle.Document);
  134. if (needsEnclosingElement)
  135. {
  136. MessagePart part = new MessagePart ();
  137. part.Name = "parameters";
  138. XmlQualifiedName qname = new XmlQualifiedName (members.ElementName, members.Namespace);
  139. if (method.Use == SoapBindingUse.Literal) part.Element = qname;
  140. else part.Type = qname;
  141. msg.Parts.Add (part);
  142. }
  143. else
  144. {
  145. for (int n=0; n<members.Count; n++)
  146. {
  147. MessagePart part = new MessagePart ();
  148. part.Name = members[n].MemberName;
  149. if (method.Use == SoapBindingUse.Literal) {
  150. if (members[n].Any)
  151. part.Type = new XmlQualifiedName ("any", members[n].Namespace);
  152. else
  153. part.Element = new XmlQualifiedName (members[n].ElementName, members[n].Namespace);
  154. }
  155. else {
  156. string namesp = members[n].TypeNamespace;
  157. if (namesp == "") namesp = members[n].Namespace;
  158. part.Type = new XmlQualifiedName (members[n].TypeName, namesp);
  159. }
  160. msg.Parts.Add (part);
  161. }
  162. }
  163. if (method.Use == SoapBindingUse.Literal)
  164. SchemaExporter.ExportMembersMapping (members);
  165. else
  166. SoapSchemaExporter.ExportMembersMapping (members, needsEnclosingElement);
  167. }
  168. protected override string ReflectMethodBinding ()
  169. {
  170. return ((SoapMethodStubInfo)MethodStubInfo).Binding;
  171. }
  172. #endregion
  173. }
  174. }