ServiceDescription.cs 9.3 KB

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