2 // System.Web.Services.Description.ServiceDescription.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
6 // Lluis Sanchez Gual (lluis@ximian.com)
\r
8 // Copyright (C) Tim Coleman, 2002
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
\r
34 using System.Reflection;
\r
35 using System.Web.Services;
\r
36 using System.Web.Services.Configuration;
\r
38 using System.Xml.Schema;
\r
39 using System.Xml.Serialization;
\r
42 using System.Collections.Generic;
45 namespace System.Web.Services.Description
\r
47 [XmlFormatExtensionPoint ("Extensions")]
\r
48 [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]
\r
49 public sealed class ServiceDescription :
\r
58 public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";
\r
60 BindingCollection bindings;
\r
61 ServiceDescriptionFormatExtensionCollection extensions;
\r
62 ImportCollection imports;
\r
63 MessageCollection messages;
\r
67 PortTypeCollection portTypes;
\r
68 string retrievalUrl;
\r
69 ServiceDescriptionCollection serviceDescriptions;
\r
70 ServiceCollection services;
\r
71 string targetNamespace;
\r
73 static ServiceDescriptionSerializer serializer;
\r
75 #endregion // Fields
\r
77 #region Constructors
\r
79 static ServiceDescription ()
\r
81 serializer = new ServiceDescriptionSerializer ();
\r
84 public ServiceDescription ()
\r
86 bindings = new BindingCollection (this);
\r
87 extensions = new ServiceDescriptionFormatExtensionCollection (this);
\r
88 imports = new ImportCollection (this);
\r
89 messages = new MessageCollection (this);
\r
91 // name = String.Empty;
\r
93 portTypes = new PortTypeCollection (this);
\r
95 serviceDescriptions = null;
\r
96 services = new ServiceCollection (this);
\r
97 targetNamespace = null;
98 types = new Types ();
\r
101 #endregion // Constructors
\r
105 [XmlElement ("import")]
\r
106 public ImportCollection Imports {
\r
107 get { return imports; }
\r
110 [XmlElement ("types")]
\r
111 public Types Types {
\r
112 get { return types; }
\r
113 set { types = value; }
\r
116 [XmlElement ("message")]
\r
117 public MessageCollection Messages {
\r
118 get { return messages; }
\r
121 [XmlElement ("portType")]
\r
122 public PortTypeCollection PortTypes {
\r
123 get { return portTypes; }
\r
126 [XmlElement ("binding")]
\r
127 public BindingCollection Bindings {
\r
128 get { return bindings; }
\r
136 ServiceDescriptionFormatExtensionCollection Extensions {
\r
137 get { return extensions; }
\r
141 [XmlAttribute ("name", DataType = "NMTOKEN")]
\r
142 public string Name {
\r
143 get { return name; }
\r
144 set { name = value; }
\r
149 public string RetrievalUrl {
\r
150 get { return retrievalUrl; }
\r
151 set { retrievalUrl = value; }
\r
155 public static XmlSerializer Serializer {
\r
156 get { return serializer; }
\r
160 public ServiceDescriptionCollection ServiceDescriptions {
\r
162 return serviceDescriptions;
\r
166 [XmlElement ("service")]
\r
167 public ServiceCollection Services {
\r
168 get { return services; }
\r
171 [XmlAttribute ("targetNamespace")]
\r
172 public string TargetNamespace {
\r
173 get { return targetNamespace; }
\r
174 set { targetNamespace = value; }
\r
177 #endregion // Properties
\r
181 public static bool CanRead (XmlReader reader)
\r
183 reader.MoveToContent ();
\r
184 return reader.LocalName == "definitions" &&
\r
185 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";
\r
188 public static ServiceDescription Read (Stream stream)
\r
190 return (ServiceDescription) serializer.Deserialize (stream);
\r
193 public static ServiceDescription Read (string fileName)
\r
195 return Read (new FileStream (fileName, FileMode.Open));
\r
198 public static ServiceDescription Read (TextReader textReader)
\r
200 return (ServiceDescription) serializer.Deserialize (textReader);
\r
203 public static ServiceDescription Read (XmlReader reader)
\r
205 return (ServiceDescription) serializer.Deserialize (reader);
\r
208 public void Write (Stream stream)
\r
210 serializer.Serialize (stream, this, GetNamespaceList ());
\r
213 public void Write (string fileName)
\r
215 Write (new FileStream (fileName, FileMode.Create));
\r
218 public void Write (TextWriter writer)
\r
220 serializer.Serialize (writer, this, GetNamespaceList ());
\r
223 public void Write (XmlWriter writer)
\r
225 serializer.Serialize (writer, this, GetNamespaceList ());
\r
228 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
\r
230 this.serviceDescriptions = serviceDescriptions;
\r
233 XmlSerializerNamespaces GetNamespaceList ()
\r
235 XmlSerializerNamespaces ns;
\r
236 ns = new XmlSerializerNamespaces ();
\r
237 ns.Add ("soap", SoapBinding.Namespace);
\r
238 ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
\r
239 ns.Add ("s", XmlSchema.Namespace);
\r
240 ns.Add ("http", HttpBinding.Namespace);
\r
241 ns.Add ("mime", MimeContentBinding.Namespace);
\r
242 ns.Add ("tm", MimeTextBinding.Namespace);
\r
243 ns.Add ("s0", TargetNamespace);
\r
245 AddExtensionNamespaces (ns, Extensions);
\r
247 if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);
\r
249 foreach (Service ser in Services)
\r
250 foreach (Port port in ser.Ports)
\r
251 AddExtensionNamespaces (ns, port.Extensions);
\r
253 foreach (Binding bin in Bindings)
\r
255 AddExtensionNamespaces (ns, bin.Extensions);
\r
256 foreach (OperationBinding op in bin.Operations)
\r
258 AddExtensionNamespaces (ns, op.Extensions);
\r
259 if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);
\r
260 if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);
\r
266 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)
\r
268 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
270 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());
\r
271 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)
\r
272 ns.Add (qname.Name, qname.Namespace);
\r
276 internal static void WriteExtensions (XmlWriter writer, object ob)
\r
278 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
279 if (extensions != null)
\r
281 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
282 WriteExtension (writer, ext);
\r
286 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)
\r
288 Type type = ext.GetType ();
\r
289 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);
\r
291 // if (prefix != null && prefix != "")
\r
292 // Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);
\r
294 // WriteStartElement (info.ElementName, info.Namespace, false);
\r
296 XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
\r
298 info.Serializer.Serialize (writer, ext, ns);
\r
301 internal static void ReadExtension (XmlDocument doc, XmlReader reader, object ob)
\r
303 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
304 if (extensions != null)
\r
306 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);
\r
309 object extension = info.Serializer.Deserialize (reader);
\r
310 extensions.Add ((ServiceDescriptionFormatExtension)extension);
\r
315 //No XmlFormatExtensionPoint attribute found
318 //Add to DocumentableItem.Extensions property
319 DocumentableItem item = ob as DocumentableItem;
325 item.Extensions.Add (doc.ReadNode (reader));
333 internal class ServiceDescriptionSerializer : XmlSerializer
\r
335 protected override void Serialize (object o, XmlSerializationWriter writer)
\r
337 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;
\r
338 xsWriter.WriteRoot_ServiceDescription (o);
\r
341 protected override object Deserialize (XmlSerializationReader reader)
\r
343 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;
\r
344 return xsReader.ReadRoot_ServiceDescription ();
347 protected override XmlSerializationWriter CreateWriter ()
\r
349 return new ServiceDescriptionWriterBase ();
\r
352 protected override XmlSerializationReader CreateReader ()
\r
354 return new ServiceDescriptionReaderBase ();
\r