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
41 namespace System.Web.Services.Description
\r
43 [XmlFormatExtensionPoint ("Extensions")]
\r
44 [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]
\r
45 public sealed class ServiceDescription :
\r
54 public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";
\r
56 BindingCollection bindings;
\r
57 ServiceDescriptionFormatExtensionCollection extensions;
\r
58 ImportCollection imports;
\r
59 MessageCollection messages;
\r
63 PortTypeCollection portTypes;
\r
64 string retrievalUrl;
\r
65 ServiceDescriptionCollection serviceDescriptions;
\r
66 ServiceCollection services;
\r
67 string targetNamespace;
\r
69 static ServiceDescriptionSerializer serializer;
\r
71 #endregion // Fields
\r
73 #region Constructors
\r
75 static ServiceDescription ()
\r
77 serializer = new ServiceDescriptionSerializer ();
\r
80 public ServiceDescription ()
\r
82 bindings = new BindingCollection (this);
\r
83 extensions = new ServiceDescriptionFormatExtensionCollection (this);
\r
84 imports = new ImportCollection (this);
\r
85 messages = new MessageCollection (this);
\r
87 name = String.Empty;
\r
89 portTypes = new PortTypeCollection (this);
\r
91 serviceDescriptions = null;
\r
92 services = new ServiceCollection (this);
\r
93 targetNamespace = String.Empty;
\r
97 #endregion // Constructors
\r
101 [XmlElement ("import")]
\r
102 public ImportCollection Imports {
\r
103 get { return imports; }
\r
106 [XmlElement ("types")]
\r
107 public Types Types {
\r
108 get { return types; }
\r
109 set { types = value; }
\r
112 [XmlElement ("message")]
\r
113 public MessageCollection Messages {
\r
114 get { return messages; }
\r
117 [XmlElement ("portType")]
\r
118 public PortTypeCollection PortTypes {
\r
119 get { return portTypes; }
\r
122 [XmlElement ("binding")]
\r
123 public BindingCollection Bindings {
\r
124 get { return bindings; }
\r
128 public ServiceDescriptionFormatExtensionCollection Extensions {
\r
129 get { return extensions; }
\r
133 [XmlAttribute ("name", DataType = "NMTOKEN")]
\r
134 public string Name {
\r
135 get { return name; }
\r
136 set { name = value; }
\r
141 public string RetrievalUrl {
\r
142 get { return retrievalUrl; }
\r
143 set { retrievalUrl = value; }
\r
147 public static XmlSerializer Serializer {
\r
148 get { return serializer; }
\r
152 public ServiceDescriptionCollection ServiceDescriptions {
\r
154 if (serviceDescriptions == null)
\r
155 throw new NullReferenceException ();
\r
156 return serviceDescriptions;
\r
160 [XmlElement ("service")]
\r
161 public ServiceCollection Services {
\r
162 get { return services; }
\r
165 [XmlAttribute ("targetNamespace")]
\r
166 public string TargetNamespace {
\r
167 get { return targetNamespace; }
\r
168 set { targetNamespace = value; }
\r
171 #endregion // Properties
\r
175 public static bool CanRead (XmlReader reader)
\r
177 reader.MoveToContent ();
\r
178 return reader.LocalName == "definitions" &&
\r
179 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";
\r
182 public static ServiceDescription Read (Stream stream)
\r
184 return (ServiceDescription) serializer.Deserialize (stream);
\r
187 public static ServiceDescription Read (string fileName)
\r
189 return Read (new FileStream (fileName, FileMode.Open));
\r
192 public static ServiceDescription Read (TextReader textReader)
\r
194 return (ServiceDescription) serializer.Deserialize (textReader);
\r
197 public static ServiceDescription Read (XmlReader reader)
\r
199 return (ServiceDescription) serializer.Deserialize (reader);
\r
202 public void Write (Stream stream)
\r
204 serializer.Serialize (stream, this, GetNamespaceList ());
\r
207 public void Write (string fileName)
\r
209 Write (new FileStream (fileName, FileMode.Create));
\r
212 public void Write (TextWriter writer)
\r
214 serializer.Serialize (writer, this, GetNamespaceList ());
\r
217 public void Write (XmlWriter writer)
\r
219 serializer.Serialize (writer, this, GetNamespaceList ());
\r
222 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
\r
224 this.serviceDescriptions = serviceDescriptions;
\r
227 XmlSerializerNamespaces GetNamespaceList ()
\r
229 XmlSerializerNamespaces ns;
\r
230 ns = new XmlSerializerNamespaces ();
\r
231 ns.Add ("soap", SoapBinding.Namespace);
\r
232 ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
\r
233 ns.Add ("s", XmlSchema.Namespace);
\r
234 ns.Add ("http", HttpBinding.Namespace);
\r
235 ns.Add ("mime", MimeContentBinding.Namespace);
\r
236 ns.Add ("tm", MimeTextBinding.Namespace);
\r
237 ns.Add ("s0", TargetNamespace);
\r
239 AddExtensionNamespaces (ns, Extensions);
\r
241 if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);
\r
243 foreach (Service ser in Services)
\r
244 foreach (Port port in ser.Ports)
\r
245 AddExtensionNamespaces (ns, port.Extensions);
\r
247 foreach (Binding bin in Bindings)
\r
249 AddExtensionNamespaces (ns, bin.Extensions);
\r
250 foreach (OperationBinding op in bin.Operations)
\r
252 AddExtensionNamespaces (ns, op.Extensions);
\r
253 if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);
\r
254 if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);
\r
260 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)
\r
262 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
264 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());
\r
265 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)
\r
266 ns.Add (qname.Name, qname.Namespace);
\r
270 internal static void WriteExtensions (XmlWriter writer, object ob)
\r
272 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
273 if (extensions != null)
\r
275 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
276 WriteExtension (writer, ext);
\r
280 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)
\r
282 Type type = ext.GetType ();
\r
283 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);
\r
285 // if (prefix != null && prefix != "")
\r
286 // Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);
\r
288 // WriteStartElement (info.ElementName, info.Namespace, false);
\r
290 XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
\r
292 info.Serializer.Serialize (writer, ext, ns);
\r
295 internal static void ReadExtension (XmlReader reader, object ob)
\r
297 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
298 if (extensions != null)
\r
300 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);
\r
303 object extension = info.Serializer.Deserialize (reader);
\r
304 extensions.Add ((ServiceDescriptionFormatExtension)extension);
\r
314 internal class ServiceDescriptionSerializer : XmlSerializer
\r
316 protected override void Serialize (object o, XmlSerializationWriter writer)
\r
318 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;
\r
319 xsWriter.WriteTree ((ServiceDescription)o);
\r
322 protected override object Deserialize (XmlSerializationReader reader)
\r
324 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;
\r
325 return xsReader.ReadTree ();
\r
328 protected override XmlSerializationWriter CreateWriter ()
\r
330 return new ServiceDescriptionWriterBase ();
\r
333 protected override XmlSerializationReader CreateReader ()
\r
335 return new ServiceDescriptionReaderBase ();
\r