2 // System.Web.Services.Description.ServiceDescription.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
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;
34 using System.Collections.Specialized;
35 using System.Reflection;
36 using System.Web.Services;
37 using System.Web.Services.Configuration;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
43 using System.Collections.Generic;
46 namespace System.Web.Services.Description
48 [XmlFormatExtensionPoint ("Extensions")]
49 [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]
50 public sealed class ServiceDescription :
59 public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";
61 BindingCollection bindings;
62 ServiceDescriptionFormatExtensionCollection extensions;
63 ImportCollection imports;
64 MessageCollection messages;
68 PortTypeCollection portTypes;
69 string retrievalUrl = String.Empty;
70 ServiceDescriptionCollection serviceDescriptions;
71 ServiceCollection services;
72 string targetNamespace;
74 static ServiceDescriptionSerializer serializer;
76 StringCollection validationWarnings;
78 static XmlSchema schema;
85 static ServiceDescription ()
87 serializer = new ServiceDescriptionSerializer ();
90 public ServiceDescription ()
92 bindings = new BindingCollection (this);
93 extensions = new ServiceDescriptionFormatExtensionCollection (this);
94 imports = new ImportCollection (this);
95 messages = new MessageCollection (this);
97 // name = String.Empty;
99 portTypes = new PortTypeCollection (this);
101 serviceDescriptions = null;
102 services = new ServiceCollection (this);
103 targetNamespace = null;
104 types = new Types ();
107 #endregion // Constructors
112 public static XmlSchema Schema {
114 if (schema == null) {
115 schema = XmlSchema.Read (typeof (ServiceDescription).Assembly.GetManifestResourceStream ("wsdl-1.1.xsd"), null);
122 [XmlElement ("import")]
123 public ImportCollection Imports {
124 get { return imports; }
127 [XmlElement ("types")]
129 get { return types; }
130 set { types = value; }
133 [XmlElement ("message")]
134 public MessageCollection Messages {
135 get { return messages; }
138 [XmlElement ("portType")]
139 public PortTypeCollection PortTypes {
140 get { return portTypes; }
143 [XmlElement ("binding")]
144 public BindingCollection Bindings {
145 get { return bindings; }
153 ServiceDescriptionFormatExtensionCollection Extensions {
154 get { return extensions; }
158 [XmlAttribute ("name", DataType = "NMTOKEN")]
161 set { name = value; }
166 public string RetrievalUrl {
167 get { return retrievalUrl; }
168 set { retrievalUrl = value; }
172 public static XmlSerializer Serializer {
173 get { return serializer; }
177 public ServiceDescriptionCollection ServiceDescriptions {
179 return serviceDescriptions;
183 [XmlElement ("service")]
184 public ServiceCollection Services {
185 get { return services; }
188 [XmlAttribute ("targetNamespace")]
189 public string TargetNamespace {
190 get { return targetNamespace; }
191 set { targetNamespace = value; }
196 public StringCollection ValidationWarnings {
197 get { return validationWarnings; }
201 #endregion // Properties
205 public static bool CanRead (XmlReader reader)
207 reader.MoveToContent ();
208 return reader.LocalName == "definitions" &&
209 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";
213 public static ServiceDescription Read (string fileName, bool validate)
216 using (XmlReader reader = XmlReader.Create (fileName)) {
217 return Read (reader, true);
220 return Read (fileName);
223 public static ServiceDescription Read (Stream stream, bool validate)
226 return Read (XmlReader.Create (stream), true);
228 return Read (stream);
231 public static ServiceDescription Read (TextReader reader, bool validate)
234 return Read (XmlReader.Create (reader), true);
236 return Read (reader);
239 public static ServiceDescription Read (XmlReader reader, bool validate)
242 StringCollection sc = new StringCollection ();
243 XmlReaderSettings s = new XmlReaderSettings ();
244 s.ValidationType = ValidationType.Schema;
245 s.Schemas.Add (Schema);
246 s.ValidationEventHandler += delegate (object o, ValidationEventArgs e) {
250 ServiceDescription ret = Read (XmlReader.Create (reader, s));
251 ret.validationWarnings = sc;
255 return Read (reader);
259 public static ServiceDescription Read (Stream stream)
261 return (ServiceDescription) serializer.Deserialize (stream);
264 public static ServiceDescription Read (string fileName)
266 return Read (new FileStream (fileName, FileMode.Open, FileAccess.Read));
269 public static ServiceDescription Read (TextReader textReader)
271 return (ServiceDescription) serializer.Deserialize (textReader);
274 public static ServiceDescription Read (XmlReader reader)
276 return (ServiceDescription) serializer.Deserialize (reader);
279 public void Write (Stream stream)
281 serializer.Serialize (stream, this, GetNamespaceList ());
284 public void Write (string fileName)
286 Write (new FileStream (fileName, FileMode.Create));
289 public void Write (TextWriter writer)
291 serializer.Serialize (writer, this, GetNamespaceList ());
294 public void Write (XmlWriter writer)
296 serializer.Serialize (writer, this, GetNamespaceList ());
299 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
301 this.serviceDescriptions = serviceDescriptions;
304 XmlSerializerNamespaces GetNamespaceList ()
306 XmlSerializerNamespaces ns;
307 ns = new XmlSerializerNamespaces ();
308 ns.Add ("soap", SoapBinding.Namespace);
310 ns.Add ("soap12", Soap12Binding.Namespace);
312 ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
313 ns.Add ("s", XmlSchema.Namespace);
314 ns.Add ("http", HttpBinding.Namespace);
315 ns.Add ("mime", MimeContentBinding.Namespace);
316 ns.Add ("tm", MimeTextBinding.Namespace);
317 ns.Add ("s0", TargetNamespace);
319 AddExtensionNamespaces (ns, Extensions);
321 if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);
323 foreach (Service ser in Services)
324 foreach (Port port in ser.Ports)
325 AddExtensionNamespaces (ns, port.Extensions);
327 foreach (Binding bin in Bindings)
329 AddExtensionNamespaces (ns, bin.Extensions);
330 foreach (OperationBinding op in bin.Operations)
332 AddExtensionNamespaces (ns, op.Extensions);
333 if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);
334 if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);
340 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)
342 foreach (object o in extensions)
344 ServiceDescriptionFormatExtension ext = o as ServiceDescriptionFormatExtension;
346 // o can be XmlElement, skipping that
349 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());
350 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)
351 ns.Add (qname.Name, qname.Namespace);
355 internal static void WriteExtensions (XmlWriter writer, object ob)
357 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
358 if (extensions != null)
360 foreach (object o in extensions) {
361 if (o is ServiceDescriptionFormatExtension)
362 WriteExtension (writer, (ServiceDescriptionFormatExtension)o);
363 else if (o is XmlElement)
364 ((XmlElement)o).WriteTo (writer);
369 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)
371 Type type = ext.GetType ();
372 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);
374 // if (prefix != null && prefix != "")
375 // Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);
377 // WriteStartElement (info.ElementName, info.Namespace, false);
379 XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
381 info.Serializer.Serialize (writer, ext, ns);
384 internal static void ReadExtension (XmlDocument doc, XmlReader reader, object ob)
386 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
387 if (extensions != null)
389 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);
392 object extension = info.Serializer.Deserialize (reader);
393 extensions.Add ((ServiceDescriptionFormatExtension)extension);
398 //No XmlFormatExtensionPoint attribute found
401 //Add to DocumentableItem.Extensions property
402 DocumentableItem item = ob as DocumentableItem;
408 item.Extensions.Add (doc.ReadNode (reader));
416 internal class ServiceDescriptionSerializer : XmlSerializer
418 protected override void Serialize (object o, XmlSerializationWriter writer)
420 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;
421 xsWriter.WriteRoot_ServiceDescription (o);
424 protected override object Deserialize (XmlSerializationReader reader)
426 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;
427 return xsReader.ReadRoot_ServiceDescription ();
430 protected override XmlSerializationWriter CreateWriter ()
432 return new ServiceDescriptionWriterBase ();
435 protected override XmlSerializationReader CreateReader ()
437 return new ServiceDescriptionReaderBase ();