SoapProtocolReflector.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. using System.Web.Services;
  11. using System.Web.Services.Protocols;
  12. using System.Xml.Serialization;
  13. using System.Xml.Schema;
  14. using System.Xml;
  15. namespace System.Web.Services.Description {
  16. internal class SoapProtocolReflector : ProtocolReflector
  17. {
  18. #region Fields
  19. internal const string EncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  20. SoapBinding soapBinding;
  21. #endregion // Fields
  22. #region Constructors
  23. public SoapProtocolReflector ()
  24. {
  25. }
  26. #endregion // Constructors
  27. #region Properties
  28. public override string ProtocolName {
  29. get { return "Soap"; }
  30. }
  31. #endregion // Properties
  32. #region Methods
  33. protected override void BeginClass ()
  34. {
  35. SoapBinding sb = new SoapBinding ();
  36. sb.Transport = SoapBinding.HttpTransport;
  37. sb.Style = ((SoapTypeStubInfo)TypeInfo).SoapBindingStyle;
  38. Binding.Extensions.Add (sb);
  39. SoapAddressBinding abind = new SoapAddressBinding ();
  40. abind.Location = ServiceUrl;
  41. Port.Extensions.Add (abind);
  42. }
  43. protected override void EndClass ()
  44. {
  45. }
  46. protected override bool ReflectMethod ()
  47. {
  48. SoapOperationBinding sob = new SoapOperationBinding();
  49. SoapMethodStubInfo method = (SoapMethodStubInfo) MethodStubInfo;
  50. sob.SoapAction = method.Action;
  51. sob.Style = method.SoapBindingStyle;
  52. OperationBinding.Extensions.Add (sob);
  53. ImportMessage (method.InputMembersMapping, InputMessage);
  54. ImportMessage (method.OutputMembersMapping, OutputMessage);
  55. AddOperationMsgBindings (method, OperationBinding.Input);
  56. AddOperationMsgBindings (method, OperationBinding.Output);
  57. foreach (HeaderInfo hf in method.Headers)
  58. {
  59. Message msg = new Message ();
  60. msg.Name = Operation.Name + hf.HeaderType.Name;
  61. MessagePart part = new MessagePart ();
  62. part.Name = hf.HeaderType.Name;
  63. msg.Parts.Add (part);
  64. ServiceDescription.Messages.Add (msg);
  65. SoapHeaderBinding hb = new SoapHeaderBinding ();
  66. hb.Message = new XmlQualifiedName (msg.Name, ServiceDescription.TargetNamespace);
  67. hb.Part = part.Name;
  68. hb.Use = method.Use;
  69. if (method.Use == SoapBindingUse.Literal)
  70. {
  71. XmlTypeMapping mapping = ReflectionImporter.ImportTypeMapping (hf.HeaderType, TypeInfo.LogicalType.WebServiceLiteralNamespace);
  72. part.Element = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  73. SchemaExporter.ExportTypeMapping (mapping);
  74. }
  75. else
  76. {
  77. XmlTypeMapping mapping = SoapReflectionImporter.ImportTypeMapping (hf.HeaderType, TypeInfo.LogicalType.WebServiceEncodedNamespace);
  78. part.Type = new XmlQualifiedName (mapping.ElementName, mapping.Namespace);
  79. SoapSchemaExporter.ExportTypeMapping (mapping);
  80. hb.Encoding = EncodingNamespace;
  81. }
  82. if ((hf.Direction & SoapHeaderDirection.Out) != 0)
  83. OperationBinding.Output.Extensions.Add (hb);
  84. if ((hf.Direction & SoapHeaderDirection.In) != 0)
  85. OperationBinding.Input.Extensions.Add (hb);
  86. }
  87. return true;
  88. }
  89. void AddOperationMsgBindings (SoapMethodStubInfo method, MessageBinding msg)
  90. {
  91. SoapBodyBinding sbbo = new SoapBodyBinding();
  92. msg.Extensions.Add (sbbo);
  93. sbbo.Use = method.Use;
  94. if (method.Use == SoapBindingUse.Encoded)
  95. {
  96. sbbo.Namespace = ServiceDescription.TargetNamespace;
  97. sbbo.Encoding = EncodingNamespace;
  98. }
  99. }
  100. void ImportMessage (XmlMembersMapping members, Message msg)
  101. {
  102. SoapMethodStubInfo method = (SoapMethodStubInfo) MethodStubInfo;
  103. bool needsEnclosingElement = (method.ParameterStyle == SoapParameterStyle.Wrapped &&
  104. method.SoapBindingStyle == SoapBindingStyle.Document);
  105. if (needsEnclosingElement)
  106. {
  107. MessagePart part = new MessagePart ();
  108. part.Name = "parameters";
  109. XmlQualifiedName qname = new XmlQualifiedName (members.ElementName, members.Namespace);
  110. if (method.Use == SoapBindingUse.Literal) part.Element = qname;
  111. else part.Type = qname;
  112. msg.Parts.Add (part);
  113. }
  114. else
  115. {
  116. for (int n=0; n<members.Count; n++)
  117. {
  118. MessagePart part = new MessagePart ();
  119. part.Name = members[n].MemberName;
  120. if (method.Use == SoapBindingUse.Literal) {
  121. part.Element = new XmlQualifiedName (members[n].MemberName, members[n].Namespace);
  122. }
  123. else {
  124. string namesp = members[n].TypeNamespace;
  125. if (namesp == "") namesp = members[n].Namespace;
  126. part.Type = new XmlQualifiedName (members[n].TypeName, namesp);
  127. }
  128. msg.Parts.Add (part);
  129. }
  130. }
  131. if (method.Use == SoapBindingUse.Literal)
  132. SchemaExporter.ExportMembersMapping (members);
  133. else
  134. SoapSchemaExporter.ExportMembersMapping (members, needsEnclosingElement);
  135. }
  136. protected override string ReflectMethodBinding ()
  137. {
  138. return ((SoapMethodStubInfo)MethodStubInfo).Binding;
  139. }
  140. #endregion
  141. }
  142. }