ServiceDescriptionReflector.cs 7.4 KB

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