ProtocolReflector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. //
  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;
  34. using System.Xml.Schema;
  35. using System.Collections;
  36. namespace System.Web.Services.Description {
  37. public abstract class ProtocolReflector {
  38. #region Fields
  39. Binding binding;
  40. string defaultNamespace;
  41. MessageCollection headerMessages;
  42. Message inputMessage;
  43. LogicalMethodInfo[] methods;
  44. Operation operation;
  45. OperationBinding operationBinding;
  46. Message outputMessage;
  47. Port port;
  48. PortType portType;
  49. string protocolName;
  50. XmlSchemaExporter schemaExporter;
  51. Service service;
  52. ServiceDescription serviceDescription;
  53. Type serviceType;
  54. string serviceUrl;
  55. SoapSchemaExporter soapSchemaExporter;
  56. MethodStubInfo methodStubInfo;
  57. TypeStubInfo typeInfo;
  58. ArrayList extensionReflectors;
  59. ServiceDescriptionReflector serviceReflector;
  60. XmlReflectionImporter reflectionImporter;
  61. SoapReflectionImporter soapReflectionImporter;
  62. CodeIdentifiers portNames;
  63. #endregion // Fields
  64. #region Constructors
  65. protected ProtocolReflector ()
  66. {
  67. defaultNamespace = WebServiceAttribute.DefaultNamespace;
  68. extensionReflectors = ExtensionManager.BuildExtensionReflectors ();
  69. }
  70. #endregion // Constructors
  71. #region Properties
  72. internal ServiceDescriptionReflector Parent {
  73. get { return serviceReflector; }
  74. }
  75. public Binding Binding {
  76. get { return binding; }
  77. }
  78. public string DefaultNamespace {
  79. get { return defaultNamespace; }
  80. }
  81. public MessageCollection HeaderMessages {
  82. get { return headerMessages; } // TODO: set
  83. }
  84. public Message InputMessage {
  85. get { return inputMessage; }
  86. }
  87. public LogicalMethodInfo Method {
  88. get { return methodStubInfo.MethodInfo; }
  89. }
  90. public WebMethodAttribute MethodAttribute {
  91. get { return methodStubInfo.MethodAttribute; }
  92. }
  93. public LogicalMethodInfo[] Methods {
  94. get { return typeInfo.LogicalType.LogicalMethods; }
  95. }
  96. public Operation Operation {
  97. get { return operation; }
  98. }
  99. public OperationBinding OperationBinding {
  100. get { return operationBinding; }
  101. }
  102. public Message OutputMessage {
  103. get { return outputMessage; }
  104. }
  105. public Port Port {
  106. get { return port; }
  107. }
  108. public PortType PortType {
  109. get { return portType; }
  110. }
  111. public abstract string ProtocolName {
  112. get;
  113. }
  114. public XmlReflectionImporter ReflectionImporter
  115. {
  116. get
  117. {
  118. if (reflectionImporter == null) {
  119. reflectionImporter = typeInfo.XmlImporter;
  120. if (reflectionImporter == null)
  121. reflectionImporter = new XmlReflectionImporter();
  122. }
  123. return reflectionImporter;
  124. }
  125. }
  126. internal SoapReflectionImporter SoapReflectionImporter
  127. {
  128. get
  129. {
  130. if (soapReflectionImporter == null) {
  131. soapReflectionImporter = typeInfo.SoapImporter;
  132. if (soapReflectionImporter == null)
  133. soapReflectionImporter = new SoapReflectionImporter();
  134. }
  135. return soapReflectionImporter;
  136. }
  137. }
  138. public XmlSchemaExporter SchemaExporter {
  139. get { return schemaExporter; }
  140. }
  141. internal SoapSchemaExporter SoapSchemaExporter {
  142. get { return soapSchemaExporter; }
  143. }
  144. public XmlSchemas Schemas {
  145. get { return serviceReflector.Schemas; }
  146. }
  147. public Service Service {
  148. get { return service; }
  149. }
  150. public ServiceDescription ServiceDescription {
  151. get { return serviceDescription; }
  152. }
  153. public ServiceDescriptionCollection ServiceDescriptions {
  154. get { return serviceReflector.ServiceDescriptions; }
  155. }
  156. public Type ServiceType {
  157. get { return serviceType; }
  158. }
  159. public string ServiceUrl {
  160. get { return serviceUrl; }
  161. }
  162. internal MethodStubInfo MethodStubInfo {
  163. get { return methodStubInfo; }
  164. }
  165. internal TypeStubInfo TypeInfo {
  166. get { return typeInfo; }
  167. }
  168. #endregion // Properties
  169. #region Methods
  170. internal void Reflect (ServiceDescriptionReflector serviceReflector, Type type, string url, XmlSchemaExporter xxporter, SoapSchemaExporter sxporter)
  171. {
  172. portNames = new CodeIdentifiers ();
  173. this.serviceReflector = serviceReflector;
  174. serviceUrl = url;
  175. serviceType = type;
  176. schemaExporter = xxporter;
  177. soapSchemaExporter = sxporter;
  178. typeInfo = TypeStubManager.GetTypeStub (type, ProtocolName);
  179. ServiceDescription desc = ServiceDescriptions [typeInfo.LogicalType.WebServiceNamespace];
  180. if (desc == null)
  181. {
  182. desc = new ServiceDescription ();
  183. desc.TargetNamespace = typeInfo.LogicalType.WebServiceNamespace;
  184. desc.Name = typeInfo.LogicalType.WebServiceName;
  185. ServiceDescriptions.Add (desc);
  186. }
  187. ImportService (desc, typeInfo, url);
  188. }
  189. void ImportService (ServiceDescription desc, TypeStubInfo typeInfo, string url)
  190. {
  191. service = desc.Services [typeInfo.LogicalType.WebServiceName];
  192. if (service == null)
  193. {
  194. service = new Service ();
  195. service.Name = typeInfo.LogicalType.WebServiceName;
  196. service.Documentation = typeInfo.LogicalType.Description;
  197. desc.Services.Add (service);
  198. }
  199. foreach (BindingInfo binfo in typeInfo.Bindings)
  200. ImportBinding (desc, service, typeInfo, url, binfo);
  201. }
  202. void ImportBinding (ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  203. {
  204. port = new Port ();
  205. port.Name = portNames.AddUnique (binfo.Name, port);
  206. bool bindingFull = true;
  207. if (binfo.Namespace != desc.TargetNamespace)
  208. {
  209. if (binfo.Location == null || binfo.Location == string.Empty)
  210. {
  211. ServiceDescription newDesc = new ServiceDescription();
  212. newDesc.TargetNamespace = binfo.Namespace;
  213. newDesc.Name = binfo.Name;
  214. bindingFull = ImportBindingContent (newDesc, typeInfo, url, binfo);
  215. if (bindingFull) {
  216. int id = ServiceDescriptions.Add (newDesc);
  217. AddImport (desc, binfo.Namespace, GetWsdlUrl (url,id));
  218. }
  219. }
  220. else {
  221. AddImport (desc, binfo.Namespace, binfo.Location);
  222. bindingFull = true;
  223. }
  224. }
  225. else
  226. bindingFull = ImportBindingContent (desc, typeInfo, url, binfo);
  227. if (bindingFull)
  228. {
  229. port.Binding = new XmlQualifiedName (binding.Name, binfo.Namespace);
  230. service.Ports.Add (port);
  231. }
  232. #if NET_2_0
  233. if (binfo.WebServiceBindingAttribute != null && binfo.WebServiceBindingAttribute.ConformsTo != WsiProfiles.None && String.IsNullOrEmpty (binfo.WebServiceBindingAttribute.Name)) {
  234. BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
  235. desc.Types.Schemas.Add (Schemas);
  236. ServiceDescriptionCollection col = new ServiceDescriptionCollection ();
  237. col.Add (desc);
  238. ConformanceCheckContext ctx = new ConformanceCheckContext (col, violations);
  239. ConformanceChecker[] checkers = WebServicesInteroperability.GetCheckers (binfo.WebServiceBindingAttribute.ConformsTo);
  240. foreach (ConformanceChecker checker in checkers) {
  241. WebServicesInteroperability.Check (ctx, checker, binding);
  242. if (violations.Count > 0)
  243. throw new InvalidOperationException (violations [0].ToString ());
  244. }
  245. }
  246. #endif
  247. }
  248. bool ImportBindingContent (ServiceDescription desc, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  249. {
  250. serviceDescription = desc;
  251. // Look for an unused name
  252. int n=0;
  253. string name = binfo.Name;
  254. bool found;
  255. do
  256. {
  257. found = false;
  258. foreach (Binding bi in desc.Bindings)
  259. if (bi.Name == name) { found = true; n++; name = binfo.Name+n; break; }
  260. }
  261. while (found);
  262. // Create the binding
  263. binding = new Binding ();
  264. binding.Name = name;
  265. binding.Type = new XmlQualifiedName (binding.Name, binfo.Namespace);
  266. #if NET_2_0
  267. if (binfo.WebServiceBindingAttribute != null && binfo.WebServiceBindingAttribute.EmitConformanceClaims) {
  268. XmlDocument doc = new XmlDocument ();
  269. XmlElement docElement = doc.CreateElement ("wsdl", "documentation", "http://schemas.xmlsoap.org/wsdl/");
  270. XmlElement claimsElement = doc.CreateElement ("wsi", "Claim", "http://ws-i.org/schemas/conformanceClaim/");
  271. claimsElement.Attributes.Append (doc.CreateAttribute ("conformsTo")).Value = "http://ws-i.org/profiles/basic/1.1";
  272. docElement.AppendChild (claimsElement);
  273. binding.DocumentationElement = docElement;
  274. }
  275. #endif
  276. portType = new PortType ();
  277. portType.Name = binding.Name;
  278. BeginClass ();
  279. foreach (SoapExtensionReflector reflector in extensionReflectors)
  280. {
  281. reflector.ReflectionContext = this;
  282. reflector.ReflectDescription ();
  283. }
  284. foreach (MethodStubInfo method in typeInfo.Methods)
  285. {
  286. methodStubInfo = method;
  287. string metBinding = ReflectMethodBinding ();
  288. if (typeInfo.GetBinding (metBinding) != binfo) continue;
  289. operation = new Operation ();
  290. operation.Name = method.OperationName;
  291. operation.Documentation = method.MethodAttribute.Description;
  292. // FIXME: SOAP 1.1 and SOAP 1.2 should share
  293. // the same message definitions.
  294. inputMessage = new Message ();
  295. inputMessage.Name = method.Name + ProtocolName + "In";
  296. ServiceDescription.Messages.Add (inputMessage);
  297. outputMessage = new Message ();
  298. outputMessage.Name = method.Name + ProtocolName + "Out";
  299. ServiceDescription.Messages.Add (outputMessage);
  300. OperationInput inOp = new OperationInput ();
  301. if (method.Name != method.OperationName) inOp.Name = method.Name;
  302. Operation.Messages.Add (inOp);
  303. inOp.Message = new XmlQualifiedName (inputMessage.Name, ServiceDescription.TargetNamespace);
  304. OperationOutput outOp = new OperationOutput ();
  305. if (method.Name != method.OperationName) outOp.Name = method.Name;
  306. Operation.Messages.Add (outOp);
  307. outOp.Message = new XmlQualifiedName (outputMessage.Name, ServiceDescription.TargetNamespace);
  308. portType.Operations.Add (operation);
  309. ImportOperationBinding ();
  310. if (!ReflectMethod ()) {
  311. #if NET_2_0
  312. // (It is somewhat hacky) If we don't
  313. // add input/output Messages, update
  314. // portType/input/@message and
  315. // porttype/output/@message.
  316. Message dupIn = Parent.MappedMessagesIn [method.MethodInfo];
  317. ServiceDescription.Messages.Remove (inputMessage);
  318. inOp.Message = new XmlQualifiedName (dupIn.Name, ServiceDescription.TargetNamespace);
  319. Message dupOut = Parent.MappedMessagesOut [method.MethodInfo];
  320. ServiceDescription.Messages.Remove (outputMessage);
  321. outOp.Message = new XmlQualifiedName (dupOut.Name, ServiceDescription.TargetNamespace);
  322. #endif
  323. }
  324. foreach (SoapExtensionReflector reflector in extensionReflectors)
  325. {
  326. reflector.ReflectionContext = this;
  327. reflector.ReflectMethod ();
  328. }
  329. }
  330. EndClass ();
  331. if (portType.Operations.Count > 0)
  332. {
  333. desc.Bindings.Add (binding);
  334. desc.PortTypes.Add (portType);
  335. return true;
  336. }
  337. else
  338. return false;
  339. }
  340. void ImportOperationBinding ()
  341. {
  342. operationBinding = new OperationBinding ();
  343. operationBinding.Name = methodStubInfo.OperationName;
  344. InputBinding inOp = new InputBinding ();
  345. operationBinding.Input = inOp;
  346. OutputBinding outOp = new OutputBinding ();
  347. operationBinding.Output = outOp;
  348. if (methodStubInfo.OperationName != methodStubInfo.Name)
  349. inOp.Name = outOp.Name = methodStubInfo.Name;
  350. binding.Operations.Add (operationBinding);
  351. }
  352. internal static void AddImport (ServiceDescription desc, string ns, string location)
  353. {
  354. Import im = new Import();
  355. im.Namespace = ns;
  356. im.Location = location;
  357. desc.Imports.Add (im);
  358. }
  359. string GetWsdlUrl (string baseUrl, int id)
  360. {
  361. return baseUrl + "?wsdl=" + id;
  362. }
  363. protected virtual void BeginClass ()
  364. {
  365. }
  366. protected virtual void EndClass ()
  367. {
  368. }
  369. public ServiceDescription GetServiceDescription (string ns)
  370. {
  371. return ServiceDescriptions [ns];
  372. }
  373. protected abstract bool ReflectMethod ();
  374. protected virtual string ReflectMethodBinding ()
  375. {
  376. return null;
  377. }
  378. #if NET_2_0
  379. [MonoNotSupported("Not Implemented")]
  380. protected virtual void ReflectDescription ()
  381. {
  382. throw new NotImplementedException ();
  383. }
  384. #endif
  385. #endregion
  386. }
  387. }