ServiceDescription.cs 9.7 KB

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