ServiceDescriptionReflector.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // System.Web.Services.Description.ServiceDescriptionReflector.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.Xml.Serialization;
  12. using System.Xml;
  13. using System.Web.Services.Protocols;
  14. namespace System.Web.Services.Description {
  15. public class ServiceDescriptionReflector {
  16. #region Fields
  17. Types types;
  18. ServiceDescriptionCollection serviceDescriptions;
  19. XmlSchemaExporter schemaExporter;
  20. #endregion // Fields
  21. #region Constructors
  22. public ServiceDescriptionReflector ()
  23. {
  24. types = new Types ();
  25. serviceDescriptions = new ServiceDescriptionCollection ();
  26. schemaExporter = new XmlSchemaExporter (types.Schemas);
  27. }
  28. #endregion // Constructors
  29. #region Properties
  30. public XmlSchemas Schemas {
  31. get { return types.Schemas; }
  32. }
  33. public ServiceDescriptionCollection ServiceDescriptions {
  34. get { return serviceDescriptions; }
  35. }
  36. #endregion // Properties
  37. #region Methods
  38. [MonoTODO]
  39. public void Reflect (Type type, string url)
  40. {
  41. ServiceDescription desc = new ServiceDescription ();
  42. serviceDescriptions.Add (desc);
  43. TypeStubInfo typeInfo = TypeStubManager.GetTypeStub (type);
  44. desc.TargetNamespace = typeInfo.WebServiceNamespace;
  45. desc.Name = typeInfo.WebServiceName;
  46. ImportService (desc, typeInfo, url);
  47. if (serviceDescriptions.Count == 1)
  48. desc.Types = types;
  49. else
  50. {
  51. foreach (ServiceDescription d in serviceDescriptions)
  52. {
  53. d.Types = new Types();
  54. for (int n=0; n<types.Schemas.Count; n++)
  55. AddImport (d, types.Schemas[n].TargetNamespace, GetSchemaUrl (url, n));
  56. }
  57. }
  58. }
  59. Service ImportService (ServiceDescription desc, TypeStubInfo typeInfo, string url)
  60. {
  61. Service service = new Service ();
  62. // service.Documentation = wsa.Description;
  63. service.Name = typeInfo.WebServiceName;
  64. desc.Services.Add (service);
  65. foreach (BindingInfo binfo in typeInfo.Bindings)
  66. ImportBinding (desc, service, typeInfo, url, binfo);
  67. return service;
  68. }
  69. void ImportBinding (ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  70. {
  71. Port port = new Port ();
  72. port.Name = binfo.Name;
  73. port.Binding = new XmlQualifiedName (binfo.Name, binfo.Namespace);
  74. service.Ports.Add (port);
  75. SoapAddressBinding abind = new SoapAddressBinding ();
  76. abind.Location = url;
  77. port.Extensions.Add (abind);
  78. if (binfo.Namespace != desc.TargetNamespace)
  79. {
  80. if (binfo.Location == null || binfo.Location == string.Empty)
  81. {
  82. ServiceDescription newDesc = new ServiceDescription();
  83. newDesc.TargetNamespace = binfo.Namespace;
  84. int id = serviceDescriptions.Add (newDesc);
  85. AddImport (desc, binfo.Namespace, GetWsdlUrl (url,id));
  86. ImportBindingContent (newDesc, typeInfo, url, binfo);
  87. }
  88. else
  89. AddImport (desc, binfo.Namespace, binfo.Location);
  90. }
  91. else
  92. ImportBindingContent (desc, typeInfo, url, binfo);
  93. }
  94. void ImportBindingContent (ServiceDescription desc, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  95. {
  96. Binding binding = new Binding ();
  97. binding.Name = binfo.Name;
  98. binding.Type = new XmlQualifiedName (binfo.Name, binfo.Namespace);
  99. desc.Bindings.Add (binding);
  100. SoapBinding sb = new SoapBinding ();
  101. sb.Transport = SoapBinding.HttpTransport;
  102. sb.Style = typeInfo.SoapBindingStyle;
  103. binding.Extensions.Add (sb);
  104. PortType ptype = new PortType ();
  105. ptype.Name = binding.Name;
  106. desc.PortTypes.Add (ptype);
  107. foreach (MethodStubInfo method in typeInfo.Methods)
  108. {
  109. if (method.Binding != binding.Name) continue;
  110. Operation oper = ImportOperation (desc, method);
  111. ptype.Operations.Add (oper);
  112. OperationBinding operb = ImportOperationBinding (desc, method);
  113. binding.Operations.Add (operb);
  114. }
  115. }
  116. Operation ImportOperation (ServiceDescription desc, MethodStubInfo method)
  117. {
  118. Operation oper = new Operation ();
  119. oper.Name = method.Name;
  120. OperationInput inOp = new OperationInput ();
  121. inOp.Message = ImportMessage (desc, oper.Name + "In", method.InputMembersMapping);
  122. oper.Messages.Add (inOp);
  123. OperationOutput outOp = new OperationOutput ();
  124. outOp.Message = ImportMessage (desc, oper.Name + "Out", method.OutputMembersMapping);
  125. oper.Messages.Add (outOp);
  126. return oper;
  127. }
  128. OperationBinding ImportOperationBinding (ServiceDescription desc, MethodStubInfo method)
  129. {
  130. OperationBinding oper = new OperationBinding ();
  131. oper.Name = method.Name;
  132. SoapOperationBinding sob = new SoapOperationBinding();
  133. sob.SoapAction = method.Action;
  134. sob.Style = method.SoapBindingStyle;
  135. oper.Extensions.Add (sob);
  136. InputBinding inOp = new InputBinding ();
  137. SoapBodyBinding sbbi = new SoapBodyBinding();
  138. sbbi.Use = method.Use;
  139. inOp.Extensions.Add (sbbi);
  140. oper.Input = inOp;
  141. OutputBinding outOp = new OutputBinding ();
  142. SoapBodyBinding sbbo = new SoapBodyBinding();
  143. sbbo.Use = method.Use;
  144. outOp.Extensions.Add (sbbo);
  145. oper.Output = outOp;
  146. return oper;
  147. }
  148. XmlQualifiedName ImportMessage (ServiceDescription desc, string name, XmlMembersMapping members)
  149. {
  150. Message msg = new Message ();
  151. msg.Name = name;
  152. MessagePart part = new MessagePart ();
  153. part.Name = "parameters";
  154. part.Element = new XmlQualifiedName (members.ElementName, members.Namespace);
  155. msg.Parts.Add (part);
  156. desc.Messages.Add (msg);
  157. schemaExporter.ExportMembersMapping (members);
  158. return new XmlQualifiedName (name, members.Namespace);
  159. }
  160. void AddImport (ServiceDescription desc, string ns, string location)
  161. {
  162. Import im = new Import();
  163. im.Namespace = ns;
  164. im.Location = location;
  165. desc.Imports.Add (im);
  166. }
  167. string GetWsdlUrl (string baseUrl, int id)
  168. {
  169. return baseUrl + "?wsdl=" + id;
  170. }
  171. string GetSchemaUrl (string baseUrl, int id)
  172. {
  173. return baseUrl + "?schema=" + id;
  174. }
  175. #endregion
  176. }
  177. }