ServiceDescription.cs 8.2 KB

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