ServiceDescription.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // System.Web.Services.Description.ServiceDescription.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.IO;
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Web.Services;
  34. using System.Web.Services.Configuration;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using System.Xml.Serialization;
  38. #if NET_2_0
  39. using System.Collections.Generic;
  40. #endif
  41. namespace System.Web.Services.Description
  42. {
  43. [XmlFormatExtensionPoint ("Extensions")]
  44. [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]
  45. public sealed class ServiceDescription :
  46. #if NET_2_0
  47. NamedItem
  48. #else
  49. DocumentableItem
  50. #endif
  51. {
  52. #region Fields
  53. public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";
  54. BindingCollection bindings;
  55. ServiceDescriptionFormatExtensionCollection extensions;
  56. ImportCollection imports;
  57. MessageCollection messages;
  58. #if !NET_2_0
  59. string name;
  60. #endif
  61. PortTypeCollection portTypes;
  62. string retrievalUrl;
  63. ServiceDescriptionCollection serviceDescriptions;
  64. ServiceCollection services;
  65. string targetNamespace;
  66. Types types;
  67. static ServiceDescriptionSerializer serializer;
  68. #endregion // Fields
  69. #region Constructors
  70. static ServiceDescription ()
  71. {
  72. serializer = new ServiceDescriptionSerializer ();
  73. }
  74. public ServiceDescription ()
  75. {
  76. bindings = new BindingCollection (this);
  77. extensions = new ServiceDescriptionFormatExtensionCollection (this);
  78. imports = new ImportCollection (this);
  79. messages = new MessageCollection (this);
  80. #if !NET_2_0
  81. // name = String.Empty;
  82. #endif
  83. portTypes = new PortTypeCollection (this);
  84. serviceDescriptions = null;
  85. services = new ServiceCollection (this);
  86. targetNamespace = String.Empty;
  87. types = null;
  88. }
  89. #endregion // Constructors
  90. #region Properties
  91. [XmlElement ("import")]
  92. public ImportCollection Imports {
  93. get { return imports; }
  94. }
  95. [XmlElement ("types")]
  96. public Types Types {
  97. get { return types; }
  98. set { types = value; }
  99. }
  100. [XmlElement ("message")]
  101. public MessageCollection Messages {
  102. get { return messages; }
  103. }
  104. [XmlElement ("portType")]
  105. public PortTypeCollection PortTypes {
  106. get { return portTypes; }
  107. }
  108. [XmlElement ("binding")]
  109. public BindingCollection Bindings {
  110. get { return bindings; }
  111. }
  112. [XmlIgnore]
  113. public
  114. #if NET_2_0
  115. override
  116. #endif
  117. ServiceDescriptionFormatExtensionCollection Extensions {
  118. get { return extensions; }
  119. }
  120. #if !NET_2_0
  121. [XmlAttribute ("name", DataType = "NMTOKEN")]
  122. public string Name {
  123. get { return name; }
  124. set { name = value; }
  125. }
  126. #endif
  127. [XmlIgnore]
  128. public string RetrievalUrl {
  129. get { return retrievalUrl; }
  130. set { retrievalUrl = value; }
  131. }
  132. [XmlIgnore]
  133. public static XmlSerializer Serializer {
  134. get { return serializer; }
  135. }
  136. [XmlIgnore]
  137. public ServiceDescriptionCollection ServiceDescriptions {
  138. get {
  139. if (serviceDescriptions == null)
  140. throw new NullReferenceException ();
  141. return serviceDescriptions;
  142. }
  143. }
  144. [XmlElement ("service")]
  145. public ServiceCollection Services {
  146. get { return services; }
  147. }
  148. [XmlAttribute ("targetNamespace")]
  149. public string TargetNamespace {
  150. get { return targetNamespace; }
  151. set { targetNamespace = value; }
  152. }
  153. #endregion // Properties
  154. #region Methods
  155. public static bool CanRead (XmlReader reader)
  156. {
  157. reader.MoveToContent ();
  158. return reader.LocalName == "definitions" &&
  159. reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";
  160. }
  161. public static ServiceDescription Read (Stream stream)
  162. {
  163. return (ServiceDescription) serializer.Deserialize (stream);
  164. }
  165. public static ServiceDescription Read (string fileName)
  166. {
  167. return Read (new FileStream (fileName, FileMode.Open));
  168. }
  169. public static ServiceDescription Read (TextReader textReader)
  170. {
  171. return (ServiceDescription) serializer.Deserialize (textReader);
  172. }
  173. public static ServiceDescription Read (XmlReader reader)
  174. {
  175. return (ServiceDescription) serializer.Deserialize (reader);
  176. }
  177. public void Write (Stream stream)
  178. {
  179. serializer.Serialize (stream, this, GetNamespaceList ());
  180. }
  181. public void Write (string fileName)
  182. {
  183. Write (new FileStream (fileName, FileMode.Create));
  184. }
  185. public void Write (TextWriter writer)
  186. {
  187. serializer.Serialize (writer, this, GetNamespaceList ());
  188. }
  189. public void Write (XmlWriter writer)
  190. {
  191. serializer.Serialize (writer, this, GetNamespaceList ());
  192. }
  193. internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
  194. {
  195. this.serviceDescriptions = serviceDescriptions;
  196. }
  197. XmlSerializerNamespaces GetNamespaceList ()
  198. {
  199. XmlSerializerNamespaces ns;
  200. ns = new XmlSerializerNamespaces ();
  201. ns.Add ("soap", SoapBinding.Namespace);
  202. ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
  203. ns.Add ("s", XmlSchema.Namespace);
  204. ns.Add ("http", HttpBinding.Namespace);
  205. ns.Add ("mime", MimeContentBinding.Namespace);
  206. ns.Add ("tm", MimeTextBinding.Namespace);
  207. ns.Add ("s0", TargetNamespace);
  208. AddExtensionNamespaces (ns, Extensions);
  209. if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);
  210. foreach (Service ser in Services)
  211. foreach (Port port in ser.Ports)
  212. AddExtensionNamespaces (ns, port.Extensions);
  213. foreach (Binding bin in Bindings)
  214. {
  215. AddExtensionNamespaces (ns, bin.Extensions);
  216. foreach (OperationBinding op in bin.Operations)
  217. {
  218. AddExtensionNamespaces (ns, op.Extensions);
  219. if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);
  220. if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);
  221. }
  222. }
  223. return ns;
  224. }
  225. void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)
  226. {
  227. foreach (ServiceDescriptionFormatExtension ext in extensions)
  228. {
  229. ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());
  230. foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)
  231. ns.Add (qname.Name, qname.Namespace);
  232. }
  233. }
  234. internal static void WriteExtensions (XmlWriter writer, object ob)
  235. {
  236. ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
  237. if (extensions != null)
  238. {
  239. foreach (ServiceDescriptionFormatExtension ext in extensions)
  240. WriteExtension (writer, ext);
  241. }
  242. }
  243. static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)
  244. {
  245. Type type = ext.GetType ();
  246. ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);
  247. // if (prefix != null && prefix != "")
  248. // Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);
  249. // else
  250. // WriteStartElement (info.ElementName, info.Namespace, false);
  251. XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
  252. ns.Add ("","");
  253. info.Serializer.Serialize (writer, ext, ns);
  254. }
  255. internal static void ReadExtension (XmlDocument doc, XmlReader reader, object ob)
  256. {
  257. ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
  258. if (extensions != null)
  259. {
  260. ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);
  261. if (info != null)
  262. {
  263. object extension = info.Serializer.Deserialize (reader);
  264. extensions.Add ((ServiceDescriptionFormatExtension)extension);
  265. return;
  266. }
  267. }
  268. //No XmlFormatExtensionPoint attribute found
  269. #if NET_2_0
  270. //Add to DocumentableItem.Extensions property
  271. DocumentableItem item = ob as DocumentableItem;
  272. if (item == null) {
  273. reader.Skip ();
  274. return;
  275. }
  276. item.Extensions.Add (doc.ReadNode (reader));
  277. #else
  278. reader.Skip ();
  279. #endif
  280. }
  281. #endregion
  282. internal class ServiceDescriptionSerializer : XmlSerializer
  283. {
  284. protected override void Serialize (object o, XmlSerializationWriter writer)
  285. {
  286. ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;
  287. xsWriter.WriteRoot_ServiceDescription (o);
  288. }
  289. protected override object Deserialize (XmlSerializationReader reader)
  290. {
  291. ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;
  292. return xsReader.ReadRoot_ServiceDescription ();
  293. }
  294. protected override XmlSerializationWriter CreateWriter ()
  295. {
  296. return new ServiceDescriptionWriterBase ();
  297. }
  298. protected override XmlSerializationReader CreateReader ()
  299. {
  300. return new ServiceDescriptionReaderBase ();
  301. }
  302. }
  303. }
  304. }