ContractReference.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.Web.Services.Discovery.ContractReference.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Dave Bettin, 2002
  9. //
  10. using System.IO;
  11. using System.Web.Services.Description;
  12. using System.Xml.Serialization;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. namespace System.Web.Services.Discovery {
  16. [XmlRootAttribute("contractRef", Namespace="http://schemas.xmlsoap.org/disco/scl/", IsNullable=true)]
  17. public class ContractReference : DiscoveryReference {
  18. #region Fields
  19. public const string Namespace = "http://schemas.xmlsoap.org/disco/scl/";
  20. private ServiceDescription contract;
  21. private string defaultFilename;
  22. private string docRef;
  23. private string href;
  24. #endregion // Fields
  25. #region Constructors
  26. public ContractReference ()
  27. {
  28. }
  29. public ContractReference (string href) : this()
  30. {
  31. this.href = href;
  32. }
  33. public ContractReference (string href, string docRef)
  34. {
  35. this.href = href;
  36. this.docRef = docRef;
  37. }
  38. #endregion // Constructors
  39. #region Properties
  40. [XmlIgnore]
  41. public ServiceDescription Contract {
  42. get {
  43. if (ClientProtocol == null)
  44. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  45. ServiceDescription desc = ClientProtocol.Documents [Url] as ServiceDescription;
  46. if (desc == null)
  47. throw new Exception ("The Documents property of ClientProtocol does not contain a WSDL document with the url " + Url);
  48. return desc;
  49. }
  50. }
  51. [XmlIgnore]
  52. public override string DefaultFilename {
  53. get { return FilenameFromUrl (Url) + ".wsdl"; }
  54. }
  55. [XmlAttribute("docRef")]
  56. public string DocRef {
  57. get { return docRef; }
  58. set { docRef = value; }
  59. }
  60. [XmlAttribute("ref")]
  61. public string Ref {
  62. get { return href; }
  63. set { href = value; }
  64. }
  65. [XmlIgnore]
  66. public override string Url {
  67. get { return href;}
  68. set { href = value; }
  69. }
  70. #endregion // Properties
  71. #region Methods
  72. public override object ReadDocument (Stream stream)
  73. {
  74. return ServiceDescription.Read (stream);
  75. }
  76. protected internal override void Resolve (string contentType, Stream stream)
  77. {
  78. ServiceDescription wsdl = ServiceDescription.Read (stream);
  79. if (!ClientProtocol.References.Contains (Url))
  80. ClientProtocol.References.Add (this);
  81. ClientProtocol.Documents.Add (Url, wsdl);
  82. ResolveInternal (ClientProtocol, wsdl);
  83. }
  84. internal void ResolveInternal (DiscoveryClientProtocol prot, ServiceDescription wsdl)
  85. {
  86. if (wsdl.Imports == null) return;
  87. foreach (Import import in wsdl.Imports)
  88. {
  89. // Make relative uris to absoulte
  90. Uri uri = new Uri (BaseUri, import.Location);
  91. string url = uri.ToString ();
  92. if (prot.Documents.Contains (url)) // Already resolved
  93. continue;
  94. try
  95. {
  96. string contentType = null;
  97. Stream stream = prot.Download (ref url, ref contentType);
  98. XmlTextReader reader = new XmlTextReader (stream);
  99. reader.MoveToContent ();
  100. DiscoveryReference refe;
  101. if (ServiceDescription.CanRead (reader))
  102. {
  103. ServiceDescription refWsdl = ServiceDescription.Read (reader);
  104. refe = new ContractReference ();
  105. refe.ClientProtocol = prot;
  106. refe.Url = url;
  107. ((ContractReference)refe).ResolveInternal (prot, refWsdl);
  108. prot.Documents.Add (url, refWsdl);
  109. }
  110. else
  111. {
  112. XmlSchema schema = XmlSchema.Read (reader, null);
  113. refe = new SchemaReference ();
  114. refe.ClientProtocol = prot;
  115. refe.Url = url;
  116. prot.Documents.Add (url, schema);
  117. }
  118. if (!prot.References.Contains (url))
  119. prot.References.Add (refe);
  120. reader.Close ();
  121. }
  122. catch (Exception ex)
  123. {
  124. ReportError (url, ex);
  125. }
  126. }
  127. }
  128. public override void WriteDocument (object document, Stream stream)
  129. {
  130. ((ServiceDescription)document).Write (stream);
  131. }
  132. #endregion // Methods
  133. }
  134. }