ProtocolReflector.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // System.Web.Services.Description.ProtocolReflector.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;
  14. using System.Xml.Schema;
  15. using System.Collections;
  16. namespace System.Web.Services.Description {
  17. public abstract class ProtocolReflector {
  18. #region Fields
  19. Binding binding;
  20. string defaultNamespace;
  21. MessageCollection headerMessages;
  22. Message inputMessage;
  23. LogicalMethodInfo[] methods;
  24. Operation operation;
  25. OperationBinding operationBinding;
  26. Message outputMessage;
  27. Port port;
  28. PortType portType;
  29. string protocolName;
  30. XmlSchemaExporter schemaExporter;
  31. Service service;
  32. ServiceDescription serviceDescription;
  33. Type serviceType;
  34. string serviceUrl;
  35. SoapSchemaExporter soapSchemaExporter;
  36. MethodStubInfo methodStubInfo;
  37. TypeStubInfo typeInfo;
  38. ArrayList extensionReflectors;
  39. ServiceDescriptionReflector serviceReflector;
  40. XmlReflectionImporter reflectionImporter;
  41. SoapReflectionImporter soapReflectionImporter;
  42. CodeIdentifiers portNames;
  43. #endregion // Fields
  44. #region Constructors
  45. protected ProtocolReflector ()
  46. {
  47. defaultNamespace = WebServiceAttribute.DefaultNamespace;
  48. extensionReflectors = ExtensionManager.BuildExtensionReflectors ();
  49. }
  50. #endregion // Constructors
  51. #region Properties
  52. public Binding Binding {
  53. get { return binding; }
  54. }
  55. public string DefaultNamespace {
  56. get { return defaultNamespace; }
  57. }
  58. public MessageCollection HeaderMessages {
  59. get { return headerMessages; } // TODO: set
  60. }
  61. public Message InputMessage {
  62. get { return inputMessage; }
  63. }
  64. public LogicalMethodInfo Method {
  65. get { return methodStubInfo.MethodInfo; }
  66. }
  67. public WebMethodAttribute MethodAttribute {
  68. get { return methodStubInfo.MethodAttribute; }
  69. }
  70. public LogicalMethodInfo[] Methods {
  71. get { return typeInfo.LogicalType.LogicalMethods; }
  72. }
  73. public Operation Operation {
  74. get { return operation; }
  75. }
  76. public OperationBinding OperationBinding {
  77. get { return operationBinding; }
  78. }
  79. public Message OutputMessage {
  80. get { return outputMessage; }
  81. }
  82. public Port Port {
  83. get { return port; }
  84. }
  85. public PortType PortType {
  86. get { return portType; }
  87. }
  88. public abstract string ProtocolName {
  89. get;
  90. }
  91. public XmlReflectionImporter ReflectionImporter
  92. {
  93. get
  94. {
  95. if (reflectionImporter == null) {
  96. reflectionImporter = typeInfo.XmlImporter;
  97. if (reflectionImporter == null)
  98. reflectionImporter = new XmlReflectionImporter();
  99. }
  100. return reflectionImporter;
  101. }
  102. }
  103. internal SoapReflectionImporter SoapReflectionImporter
  104. {
  105. get
  106. {
  107. if (soapReflectionImporter == null) {
  108. soapReflectionImporter = typeInfo.SoapImporter;
  109. if (soapReflectionImporter == null)
  110. soapReflectionImporter = new SoapReflectionImporter();
  111. }
  112. return soapReflectionImporter;
  113. }
  114. }
  115. public XmlSchemaExporter SchemaExporter {
  116. get { return schemaExporter; }
  117. }
  118. internal SoapSchemaExporter SoapSchemaExporter {
  119. get { return soapSchemaExporter; }
  120. }
  121. public XmlSchemas Schemas {
  122. get { return serviceReflector.Schemas; }
  123. }
  124. public Service Service {
  125. get { return service; }
  126. }
  127. public ServiceDescription ServiceDescription {
  128. get { return serviceDescription; }
  129. }
  130. public ServiceDescriptionCollection ServiceDescriptions {
  131. get { return serviceReflector.ServiceDescriptions; }
  132. }
  133. public Type ServiceType {
  134. get { return serviceType; }
  135. }
  136. public string ServiceUrl {
  137. get { return serviceUrl; }
  138. }
  139. internal MethodStubInfo MethodStubInfo {
  140. get { return methodStubInfo; }
  141. }
  142. internal TypeStubInfo TypeInfo {
  143. get { return typeInfo; }
  144. }
  145. #endregion // Properties
  146. #region Methods
  147. internal void Reflect (ServiceDescriptionReflector serviceReflector, Type type, string url, XmlSchemaExporter xxporter, SoapSchemaExporter sxporter)
  148. {
  149. portNames = new CodeIdentifiers ();
  150. this.serviceReflector = serviceReflector;
  151. serviceUrl = url;
  152. serviceType = type;
  153. schemaExporter = xxporter;
  154. soapSchemaExporter = sxporter;
  155. typeInfo = TypeStubManager.GetTypeStub (type, ProtocolName);
  156. ServiceDescription desc = ServiceDescriptions [typeInfo.LogicalType.WebServiceNamespace];
  157. if (desc == null)
  158. {
  159. desc = new ServiceDescription ();
  160. desc.TargetNamespace = typeInfo.LogicalType.WebServiceNamespace;
  161. desc.Name = typeInfo.LogicalType.WebServiceName;
  162. ServiceDescriptions.Add (desc);
  163. }
  164. ImportService (desc, typeInfo, url);
  165. }
  166. void ImportService (ServiceDescription desc, TypeStubInfo typeInfo, string url)
  167. {
  168. service = desc.Services [typeInfo.LogicalType.WebServiceName];
  169. if (service == null)
  170. {
  171. service = new Service ();
  172. service.Name = typeInfo.LogicalType.WebServiceName;
  173. service.Documentation = typeInfo.LogicalType.Description;
  174. desc.Services.Add (service);
  175. }
  176. foreach (BindingInfo binfo in typeInfo.Bindings)
  177. ImportBinding (desc, service, typeInfo, url, binfo);
  178. }
  179. void ImportBinding (ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  180. {
  181. port = new Port ();
  182. port.Name = portNames.AddUnique (binfo.Name, port);
  183. bool bindingFull = true;
  184. if (binfo.Namespace != desc.TargetNamespace)
  185. {
  186. if (binfo.Location == null || binfo.Location == string.Empty)
  187. {
  188. ServiceDescription newDesc = new ServiceDescription();
  189. newDesc.TargetNamespace = binfo.Namespace;
  190. newDesc.Name = binfo.Name;
  191. bindingFull = ImportBindingContent (newDesc, typeInfo, url, binfo);
  192. if (bindingFull) {
  193. int id = ServiceDescriptions.Add (newDesc);
  194. AddImport (desc, binfo.Namespace, GetWsdlUrl (url,id));
  195. }
  196. }
  197. else {
  198. AddImport (desc, binfo.Namespace, binfo.Location);
  199. bindingFull = true;
  200. }
  201. }
  202. else
  203. bindingFull = ImportBindingContent (desc, typeInfo, url, binfo);
  204. if (bindingFull)
  205. {
  206. port.Binding = new XmlQualifiedName (binding.Name, binfo.Namespace);
  207. service.Ports.Add (port);
  208. }
  209. }
  210. bool ImportBindingContent (ServiceDescription desc, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  211. {
  212. serviceDescription = desc;
  213. // Look for an unused name
  214. int n=0;
  215. string name = binfo.Name;
  216. bool found;
  217. do
  218. {
  219. found = false;
  220. foreach (Binding bi in desc.Bindings)
  221. if (bi.Name == name) { found = true; n++; name = binfo.Name+n; break; }
  222. }
  223. while (found);
  224. // Create the binding
  225. binding = new Binding ();
  226. binding.Name = name;
  227. binding.Type = new XmlQualifiedName (binding.Name, binfo.Namespace);
  228. portType = new PortType ();
  229. portType.Name = binding.Name;
  230. BeginClass ();
  231. foreach (MethodStubInfo method in typeInfo.Methods)
  232. {
  233. methodStubInfo = method;
  234. string metBinding = ReflectMethodBinding ();
  235. if (typeInfo.GetBinding (metBinding) != binfo) continue;
  236. operation = new Operation ();
  237. operation.Name = method.OperationName;
  238. operation.Documentation = method.MethodAttribute.Description;
  239. inputMessage = new Message ();
  240. inputMessage.Name = method.Name + ProtocolName + "In";
  241. ServiceDescription.Messages.Add (inputMessage);
  242. outputMessage = new Message ();
  243. outputMessage.Name = method.Name + ProtocolName + "Out";
  244. ServiceDescription.Messages.Add (outputMessage);
  245. OperationInput inOp = new OperationInput ();
  246. if (method.Name != method.OperationName) inOp.Name = method.Name;
  247. Operation.Messages.Add (inOp);
  248. inOp.Message = new XmlQualifiedName (inputMessage.Name, ServiceDescription.TargetNamespace);
  249. OperationOutput outOp = new OperationOutput ();
  250. if (method.Name != method.OperationName) outOp.Name = method.Name;
  251. Operation.Messages.Add (outOp);
  252. outOp.Message = new XmlQualifiedName (outputMessage.Name, ServiceDescription.TargetNamespace);
  253. portType.Operations.Add (operation);
  254. ImportOperationBinding ();
  255. ReflectMethod ();
  256. foreach (SoapExtensionReflector reflector in extensionReflectors)
  257. {
  258. reflector.ReflectionContext = this;
  259. reflector.ReflectMethod ();
  260. }
  261. }
  262. EndClass ();
  263. if (portType.Operations.Count > 0)
  264. {
  265. desc.Bindings.Add (binding);
  266. desc.PortTypes.Add (portType);
  267. return true;
  268. }
  269. else
  270. return false;
  271. }
  272. void ImportOperationBinding ()
  273. {
  274. operationBinding = new OperationBinding ();
  275. operationBinding.Name = methodStubInfo.OperationName;
  276. InputBinding inOp = new InputBinding ();
  277. operationBinding.Input = inOp;
  278. OutputBinding outOp = new OutputBinding ();
  279. operationBinding.Output = outOp;
  280. if (methodStubInfo.OperationName != methodStubInfo.Name)
  281. inOp.Name = outOp.Name = methodStubInfo.Name;
  282. binding.Operations.Add (operationBinding);
  283. }
  284. internal static void AddImport (ServiceDescription desc, string ns, string location)
  285. {
  286. Import im = new Import();
  287. im.Namespace = ns;
  288. im.Location = location;
  289. desc.Imports.Add (im);
  290. }
  291. string GetWsdlUrl (string baseUrl, int id)
  292. {
  293. return baseUrl + "?wsdl=" + id;
  294. }
  295. protected virtual void BeginClass ()
  296. {
  297. }
  298. protected virtual void EndClass ()
  299. {
  300. }
  301. public ServiceDescription GetServiceDescription (string ns)
  302. {
  303. return ServiceDescriptions [ns];
  304. }
  305. protected abstract bool ReflectMethod ();
  306. protected virtual string ReflectMethodBinding ()
  307. {
  308. return null;
  309. }
  310. #endregion
  311. }
  312. }