ServiceDescription.cs 9.4 KB

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