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
70 static ServiceDescriptionSerializer serializer;
\r
72 static ServiceDescriptionSerializer serializer {
74 return (ServiceDescriptionSerializer)AppDomain.CurrentDomain.GetData("ServiceDescriptionSerializer.serializer");
77 AppDomain.CurrentDomain.SetData("ServiceDescriptionSerializer.serializer", value);
82 #endregion // Fields
\r
84 #region Constructors
\r
86 static ServiceDescription ()
\r
88 serializer = new ServiceDescriptionSerializer ();
\r
91 public ServiceDescription ()
\r
93 bindings = new BindingCollection (this);
\r
94 extensions = new ServiceDescriptionFormatExtensionCollection (this);
\r
95 imports = new ImportCollection (this);
\r
96 messages = new MessageCollection (this);
\r
98 // name = String.Empty;
\r
100 portTypes = new PortTypeCollection (this);
\r
102 serviceDescriptions = null;
\r
103 services = new ServiceCollection (this);
\r
104 targetNamespace = String.Empty;
\r
108 #endregion // Constructors
\r
112 [XmlElement ("import")]
\r
113 public ImportCollection Imports {
\r
114 get { return imports; }
\r
117 [XmlElement ("types")]
\r
118 public Types Types {
\r
119 get { return types; }
\r
120 set { types = value; }
\r
123 [XmlElement ("message")]
\r
124 public MessageCollection Messages {
\r
125 get { return messages; }
\r
128 [XmlElement ("portType")]
\r
129 public PortTypeCollection PortTypes {
\r
130 get { return portTypes; }
\r
133 [XmlElement ("binding")]
\r
134 public BindingCollection Bindings {
\r
135 get { return bindings; }
\r
139 public ServiceDescriptionFormatExtensionCollection Extensions {
\r
140 get { return extensions; }
\r
144 [XmlAttribute ("name", DataType = "NMTOKEN")]
\r
145 public string Name {
\r
146 get { return name; }
\r
147 set { name = value; }
\r
152 public string RetrievalUrl {
\r
153 get { return retrievalUrl; }
\r
154 set { retrievalUrl = value; }
\r
158 public static XmlSerializer Serializer {
\r
159 get { return serializer; }
\r
163 public ServiceDescriptionCollection ServiceDescriptions {
\r
165 if (serviceDescriptions == null)
\r
166 throw new NullReferenceException ();
\r
167 return serviceDescriptions;
\r
171 [XmlElement ("service")]
\r
172 public ServiceCollection Services {
\r
173 get { return services; }
\r
176 [XmlAttribute ("targetNamespace")]
\r
177 public string TargetNamespace {
\r
178 get { return targetNamespace; }
\r
179 set { targetNamespace = value; }
\r
182 #endregion // Properties
\r
186 public static bool CanRead (XmlReader reader)
\r
188 reader.MoveToContent ();
\r
189 return reader.LocalName == "definitions" &&
\r
190 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";
\r
193 public static ServiceDescription Read (Stream stream)
\r
195 return (ServiceDescription) serializer.Deserialize (stream);
\r
198 public static ServiceDescription Read (string fileName)
\r
200 return Read (new FileStream (fileName, FileMode.Open));
\r
203 public static ServiceDescription Read (TextReader textReader)
\r
205 return (ServiceDescription) serializer.Deserialize (textReader);
\r
208 public static ServiceDescription Read (XmlReader reader)
\r
210 return (ServiceDescription) serializer.Deserialize (reader);
\r
213 public void Write (Stream stream)
\r
215 serializer.Serialize (stream, this, GetNamespaceList ());
\r
218 public void Write (string fileName)
\r
220 Write (new FileStream (fileName, FileMode.Create));
\r
223 public void Write (TextWriter writer)
\r
225 serializer.Serialize (writer, this, GetNamespaceList ());
\r
228 public void Write (XmlWriter writer)
\r
230 serializer.Serialize (writer, this, GetNamespaceList ());
\r
233 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
\r
235 this.serviceDescriptions = serviceDescriptions;
\r
238 XmlSerializerNamespaces GetNamespaceList ()
\r
240 XmlSerializerNamespaces ns;
\r
241 ns = new XmlSerializerNamespaces ();
\r
242 ns.Add ("soap", SoapBinding.Namespace);
\r
243 ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
\r
244 ns.Add ("s", XmlSchema.Namespace);
\r
245 ns.Add ("http", HttpBinding.Namespace);
\r
246 ns.Add ("mime", MimeContentBinding.Namespace);
\r
247 ns.Add ("tm", MimeTextBinding.Namespace);
\r
248 ns.Add ("s0", TargetNamespace);
\r
250 AddExtensionNamespaces (ns, Extensions);
\r
252 if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);
\r
254 foreach (Service ser in Services)
\r
255 foreach (Port port in ser.Ports)
\r
256 AddExtensionNamespaces (ns, port.Extensions);
\r
258 foreach (Binding bin in Bindings)
\r
260 AddExtensionNamespaces (ns, bin.Extensions);
\r
261 foreach (OperationBinding op in bin.Operations)
\r
263 AddExtensionNamespaces (ns, op.Extensions);
\r
264 if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);
\r
265 if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);
\r
271 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)
\r
273 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
275 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());
\r
276 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)
\r
277 ns.Add (qname.Name, qname.Namespace);
\r
281 internal static void WriteExtensions (XmlWriter writer, object ob)
\r
283 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
284 if (extensions != null)
\r
286 foreach (ServiceDescriptionFormatExtension ext in extensions)
\r
287 WriteExtension (writer, ext);
\r
291 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)
\r
293 Type type = ext.GetType ();
\r
294 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);
\r
296 // if (prefix != null && prefix != "")
\r
297 // Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);
\r
299 // WriteStartElement (info.ElementName, info.Namespace, false);
\r
301 XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
\r
303 info.Serializer.Serialize (writer, ext, ns);
\r
306 internal static void ReadExtension (XmlReader reader, object ob)
\r
308 ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);
\r
309 if (extensions != null)
\r
311 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);
\r
314 object extension = info.Serializer.Deserialize (reader);
\r
315 extensions.Add ((ServiceDescriptionFormatExtension)extension);
\r
325 internal class ServiceDescriptionSerializer : XmlSerializer
\r
327 protected override void Serialize (object o, XmlSerializationWriter writer)
\r
329 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;
\r
330 xsWriter.WriteTree ((ServiceDescription)o);
\r
333 protected override object Deserialize (XmlSerializationReader reader)
\r
335 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;
\r
336 return xsReader.ReadTree ();
\r
339 protected override XmlSerializationWriter CreateWriter ()
\r
341 return new ServiceDescriptionWriterBase ();
\r
344 protected override XmlSerializationReader CreateReader ()
\r
346 return new ServiceDescriptionReaderBase ();
\r